In Type system§
See primary documentation in context for submethod BUILD.
The submethod BUILD
is (indirectly) called by .bless. It is meant to set private and public attributes of a class and receives all named attributes passed into .bless
. The default constructor .new defined in Mu
is the method that invokes it. Given that public accessor methods are not available in BUILD
, you must use private attribute notation instead.
class C { has $.attr; submethod BUILD (:$attr = 42) { $!attr = $attr }; multi method new($positional) { self.bless(:attr($positional), |%_) } }; C.new.say; C.new('answer').say; # OUTPUT: «C.new(attr => 42) # C.new(attr => "answer")»