Skip to content

Instantly share code, notes, and snippets.

@sdondley
Created January 27, 2023 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdondley/48739e6624894d1f9b60827e29435919 to your computer and use it in GitHub Desktop.
Save sdondley/48739e6624894d1f9b60827e29435919 to your computer and use it in GitHub Desktop.
A collection of useful Raku snippets
# 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