Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are private attributes hidden by default by .perl and .gist

Tags:

oop

raku

That seems to be the case:

class Foo { has $!bar }; say Foo.new( :3bar ).perl # OUTPUT: «Foo.new␤» 

Documentation says it's implementation dependent, but I wonder if this actually makes sense.

like image 866
jjmerelo Avatar asked Feb 26 '19 13:02

jjmerelo


1 Answers

The .perl output is correct. Foo.new( :3bar ) does not do what you think. If you add a method bar() { $!bar }, you'll notice that the private attribute $!bar did not get set:

class Foo {
    has $!bar;
    method bar() { $!bar }
}
say Foo.new( :3bar ).bar;   # (Any)
say Foo.new( :3bar ).perl;  # Foo.new

The named parameter :3bar (aka bar => 3) is silently ignored, because there is no public attribute with the name bar.

If you want it to complain about this situation, then maybe https://modules.raku.org/dist/StrictNamedArguments is something for you.

like image 192
Elizabeth Mattijsen Avatar answered Dec 06 '22 00:12

Elizabeth Mattijsen