3 ms·
It mixes in additional features to a given instance my $old = 0; my $val = $old but True; say $old.Bool; # False say $val.Bool; # True s
by b2gills 9y ago
It mixes in additional features to a given instance
my $old = 0;
my $val = $old but True;
say $old.Bool; # False
say $val.Bool; # True
say $val.Numeric; # 0
Which can be thought of as short for
my $old = 0;
my $val = class :: is Int {
method Bool (){ True }
}.new( $old )
say $old.Bool; # False
say $val.Bool; # True
say $val.Numeric; # 0
Note that under the hood it is a little more complicated so that it will work on any instance.
I think this was added as a generalization which replaces the following in Perl 5.
my $val = "0 but true";
say $val ? "True" : "False"; # True
say 0+$val; # 0