trait is rw

Documentation for trait is rw assembled from the following types:

language documentation Type system

From Type system

(Type system) trait is rw

Defined as:

sub trait_mod:<is>(Mu:U $type:$rw!)

The trait is rw on a class will create writable accessor methods on all public attributes of that class.

class C is rw {
    has $.a;
my $c = = 42;
say $c# OUTPUT: «42␤» 

class Attribute

From Attribute

(Attribute) trait is rw

Defined as:

multi sub trait_mod:<is> (Attribute:D $attr:$rw!)

Marks an attribute as read/write as opposed to the default readonly. The default accessor for the attribute will return a writable value.

class Boo {
   has $.bar is rw;
   has $.baz;
my $boo =;
$ = 42# works 
$boo.baz = 42;
CATCH { default { put .^name''.Str } };
# OUTPUT: «X::Assignment::RO: Cannot modify an immutable Any␤» 

class Routine

From Routine

(Routine) trait is rw

multi sub trait_mod:<is>(Routine $r:$rw!)

When a routine is modified with this trait, its return value will be writable. This is useful when returning variables or writable elements of hashes or arrays, for example:

sub walk(\thing*@keysis rw {
    my $current := thing;
    for @keys -> $k {
        if $k ~~ Int {
            $current := $current[$k];
        else {
            $current := $current{$k};
my %hash;
walk(%hash'some''key'12= 'autovivified';
say %hash.perl;


("some" => {"key" => [Any, [AnyAny"autovivified"]]}).hash

Note that return marks return values as read only; if you need an early exit from an is rw routine, you have to use return-rw instead.