Skip to content

Instantly share code, notes, and snippets.

@pierre-vigier
Created January 13, 2016 15:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pierre-vigier/3da087040fddd8f0449b to your computer and use it in GitHub Desktop.
use v6;
multi trait_mod:<is>(Attribute:D $attr, :$providing-private-accessor! ) is export {
my $class = $attr.package;
my $name = $attr.name.substr(2); #drop the initial $!
if $name ~~ $class.^private_method_table {
die "A private method with attribute name already exists, can't create private accessor";
} else {
$class.^add_private_method($name, method (Mu:D:) {
$attr.get-value( self );
});
}
}
my class A {
has $!private is providing-private-accessor = 'test';
method test(A:D: A $b) {
say "Private value from the other class : "~$b!private;
}
}
my $a = A.new();
my $b = A.new( private => 12 );
$a.test( $b );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment