Raku - default member values... oh my!

TL;DR

Reddit answered, and itā€™s full of juice!

So here we are again! Not happy with my first stab (Raku - default member values), I discovered the documentation for initializing objects (Raku - default member values, again).

So weā€™re done, right?

Wellā€¦ not so fast folks! Reddit answered, at itā€™s very, very interesting.

User codesections points out that thereā€™s yet another arrow to shoot, i.e. trait is built:

By default, this trait allows setting up a private attribute during object construction via .new.

As I understand, it solves the problem of making it possible to initialize private variables upon instantiation (tieing same-named parameters). This, paired with the default on the spot, gives us a clean and clear way to do the initialization in the simple cases.

Adapting the example in the docs a bit:

class Foo {
    has $!bar is built = 'default';
    method bar { $!bar; }
}
put Foo.new.bar;                  # Ā«defaultā¤Ā» 
put Foo.new(bar => 'custom').bar; # Ā«customā¤Ā» 

Reddit user b2gills goes beyond by gently adapting the example in previous posts to make use of is built and also suggest how to do post-initialization customizations. The answer is a highly suggested read andā€¦ spoiler: itā€™s TWEAK.

The good thing is that the initialization of attributes from defaults and constructor inputs are now decoupled from these customizations, so we donā€™t have to specify them explicitly any more, which is much less error prone and future proof as b2gills rightly observes.

Hence, the following works as expected and no repetition/explicit initialization is necessary:

class Foo {
    has $!bar is built = 'default';
    submethod TWEAK () { $!bar = $!bar.uc if $!bar !~~ /^default/; }
    method bar { $!bar; }
}
put Foo.new.bar;                  # Ā«defaultā¤Ā» 
put Foo.new(bar => 'custom').bar; # Ā«CUSTOMā¤Ā» 

With all of this, I do agree with b2gillsā€™s summary:

Writing TWEAK should be a rare occurrence.

Writing BUILD should be doubly rare.

I guess Iā€™ll have to revise a few classesā€¦ it will be a useful exercise to fix this in memory šŸ˜…


Comments? Octodon, , GitHub, Reddit, or drop me a line!