In role Associative§

See primary documentation in context for method STORE

method STORE(\values:$INITIALIZE)

This method should only be supplied if you want to support the:

my %h is Foo = => 42=> 666;

syntax for binding your implementation of the Associative role.

Should accept the values to (re-)initialize the object with, which either could consist of Pairs, or separate key/value pairs. The optional named parameter will contain a True value when the method is called on the object for the first time. Should return the invocant.

In Subscripts§

See primary documentation in context for method STORE

method STORE (::?CLASS:D: \values:$INITIALIZE)

This method should only be supplied if you want to support this syntax:

my @a is Foo = 1,2,3;

Which is used for binding your implementation of the Positional role.

STORE should accept the values to (re-)initialize the object with. The optional named parameter will contain a True value when the method is called on the object for the first time. It should return the invocant.

role Logger { method logStr $msg{}}
 
class ConsoLogger does Logger { method log ( Str $msg ) { "❢ $msg".say }}
 
class DNA {
    has $.chain;
    has Logger $!logger;
 
    submethod BUILD:$chain:$logger = ConsoLogger.new() ) {}
 
    method STORE (Str $chain where {
            /^^ <[ACGT]>+ $$ / and
            .chars %% 3
        },
        :$INITIALIZE --> DNA{
 
        if ($INITIALIZE{
            $!logger = ConsoLogger.new();
            $!logger.log"Initialized" );
        }
 
        $!chain  := $chain;
        $!logger.log("Change value to $chain" );
        self
    }
 
    method Str(::?CLASS:D:{ return $!chain.comb.rotor(3).map*.join("")).join("|"}
};
 
my @string is DNA = 'GAATCC';    # OUTPUT: «❢ Initialized␤❢ Change value to GAATCC␤» 
say ~@string;                    # OUTPUT: «GAA|TCC␤» 
@string = 'ACGTCG';              # OUTPUT: «❢ Change value to ACGTCG␤» 
say  ~@string;                   # OUTPUT: «ACG|TCG␤» 

This code takes into account the value of $INITIALIZE, which is set to True only if we are assigning a value to a variable declared using the is syntax for the first time; for instance, as in this case, we might need to initialize any injected dependency. The STORE method should set the self variable and return it in all cases, including when the variable has already been initialized; however, only in the first case we need to initialize the logger we are using in this example.

The presence of the INITIALIZE flag can be also used to create immutable data structures:

class A {
    has @.foo handles <Str gist raku>;
    multi method STORE(*@!foo:$INITIALIZE!{ }
    multi method STORE(|) { die "Immutable" }
}
 
my @a is A = 1,2,3,4;
say @a;        # OUTPUT: «[1,2,3,4]␤» 
@a = 4,5,6,7;  # dies: Immutable 

In Subscripts§

See primary documentation in context for method STORE 0

method STORE (::?CLASS:D: \values:$INITIALIZE)

This method should only be supplied if you want to support the:

my %h is Foo = => 42=> 666;

syntax for binding your implementation of the Associative role.

Should accept the values to (re-)initialize the object with, which either could consist of Pairs, or separate key/value pairs. The optional named parameter will contain a True value when the method is called on the object for the first time. Should return the invocant.

In role Positional§

See primary documentation in context for method STORE

method STORE(\values:$INITIALIZE)

This method should only be supplied if you want to support the:

my @a is Foo = 1,2,3;

syntax for binding your implementation of the Positional role.

Should accept the values to (re-)initialize the object with. The optional named parameter will contain a True value when the method is called on the object for the first time. Should return the invocant.