Created
January 27, 2023 21:22
-
-
Save sdondley/48739e6624894d1f9b60827e29435919 to your computer and use it in GitHub Desktop.
A collection of useful Raku snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# My Raku Recipes and Idioms | |
Here's a collection of Raku code snippets I use with explanations. Maybe you'll | |
also find them useful or If you find them useful you know better ways to accomplish things, | |
just let me know in the comments. | |
## Object construction | |
### Simpler construction | |
One thing I don't like about object construction in Raku is by default | |
you have to pass in a Pair object: `Blah.new(attr => 'blah')`. This is fine, | |
especially if you need to initialize a lot of attributes. But for objects | |
that take one obvious argument, it's overkill and you just want to do | |
`Blah.new('blah')`. But at the same time, you don't want to remove the longer Pair | |
notation. | |
So here's a way to get the best of both worlds: | |
```raku | |
class Box { | |
has $!data; | |
method new( $d = '', :$data = $d ) { | |
self.bless(:$data); | |
} | |
} | |
The compiler won't blink and the program will silently continue executing as if nothing is wrong. | |
my $box = Box.new('hi'); | |
#say $box.data; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment