Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

71 lines (55 sloc) 1.478 kb
use v6;
use MONKEY_TYPING;
use Test;
plan 8;
# L<S12/Open vs Closed Classes>
class Something {
has $.attribute;
method in_Something { 'a' ~ $.attribute };
}
my $x = Something.new(attribute => 'b');
is $x.in_Something, 'ab', 'basic OO sanity';
# although we use curlies here to be better fudge-able, remeber
# that 'augment' class extensions are *not* lexically scoped
{
augment class Something {
method later_added {
'later'
}
method uses-other-methods {
'blubb|' ~ self.in_Something;
}
}
my $y = Something.new(attribute => 'c');
is $y.later_added, 'later', 'can call method that was later added';
is $y.uses-other-methods, 'blubb|ac', 'can call new method that calls other methods';
is $x.later_added, 'later', 'can call method on object that was instantiated earlier';
is $x.uses-other-methods, 'blubb|ab', 'works with other method too';
}
# now try to extend "core" types
# RT #75114
{
augment class Str {
method mydouble {
self.uc ~ self.lc;
}
}
is 'aBc'.mydouble, 'ABCabc', 'can extend Str';
}
# RT #75114
{
augment class Int {
method triple { self * 3 }
}
is 3.triple, 9, 'can extend Int';
}
{
augment class Array {
method last-and-first {
self[self - 1] ~ self[0]
}
}
my @a = 1, 3, 7, 0;
is @a.last-and-first, '01', 'can extend class Array';
}
# vim: ft=perl6
Jump to Line
Something went wrong with that request. Please try again.