submethod BUILD

Documentation for submethod BUILD assembled from the following types:

language documentation Type system

From Type system

(Type system) 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 names 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.sayC.new('answer').say;
# OUTPUT: «C.new(attr => 42)␤ 
#          C.new(attr => "answer")␤»