ETOOBUSY š minimal blogging for the impatient
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 š