Skip to content

Instantly share code, notes, and snippets.

@cjfields
Created September 8, 2014 02:39
Show Gist options
  • Save cjfields/df13dd4df74f0b0ca2a5 to your computer and use it in GitHub Desktop.
Save cjfields/df13dd4df74f0b0ca2a5 to your computer and use it in GitHub Desktop.
Perl6 Alias trait
[cjfields@Chriss-MacBook-Pro bioperl6 (master)]$ prove6 -l -v t/Role/Aliased.t
t/Role/Aliased.t ..
ok 1 -
not ok 2 - adds alias
# Failed test 'adds alias'
# at t/Role/Aliased.t line 23
ok 3 -
1..3
# Looks like you failed 1 tests of 3
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/3 subtests
Test Summary Report
-------------------
t/Role/Aliased.t (Wstat: 256 Tests: 3 Failed: 1)
Failed test: 2
Non-zero exit status: 1
Files=1, Tests=3, 0 wallclock secs ( 0.02 usr 0.01 sys + 0.31 cusr 0.05 csys = 0.39 CPU)
Result: FAIL
use v6;
use Test;
multi trait_mod:<is>(Attribute:D $attr, :$alias!) {
my $accessor_name = $attr.name.substr(2);
for $alias.list -> $name {
$attr.HOW.add_method($attr.HOW, $name, method {
self.$accessor_name
} );
}
}
class Stub {
has $.a is alias('foo');
has $.long_name is alias('bar');
};
my $test = Stub.new(:a<hi there>, :long_name<Long time no see>);
is($test.a, 'hi there', '');
ok($test.can('foo'), 'adds alias');
# Does not work, get "No such method 'foo' for invocant of type 'Stub'"
#is($test.foo, 'hi there');
is($test.long_name, 'Long time no see');
done();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment