You can use the monkey to enter text Int . Expect that the optimizer is a guarantee, your will / boobs are compressed, and the world ends in general. The skill of the monkey is evil. Do not use it if you do not need to.
use MONKEY-TYPING; augment class Int { method sq(Int:D $i:){ $i * $i } }; my Int $i = 4; say $i.sq;
You can mix a role with an object. Note that the object is an object, not an Int class and a $i container.
my $i = 4 but role :: { method sq(Int:D $i:){ $i * $i } }; say $i.sq;
You can create a free floating method and use the method call operator .& .
my method sq(Int:D $i:){ $i * $i }; my $i = 4; say $i.&sq;
EDIT:
If you really want to break assumptions, you can even access private attributes.
class Foo { has $!bar = 'meow'; }; use MONKEY-TYPING; augment class Foo { method baz { say $!bar } }; Foo.new.baz
user5854207
source share