/gist:3da087040fddd8f0449b Secret
Created
January 13, 2016 15:01
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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