私有属性默认是由.perl和.gist隐藏的

问题描述 投票:6回答:1

情况似乎是这样的:

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

文档说it's implementation dependent,但我想知道这是否真的有意义。

oop perl6
1个回答
9
投票

.perl输出是正确的。 Foo.new( :3bar )不按你的想法行事。如果你添加一个method bar() { $!bar },你会发现私有属性$!bar没有设置:

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

命名参数:3bar(aka bar => 3)被默默忽略,因为没有名为bar的公共属性。

如果你想让它抱怨这种情况,那么也许https://modules.perl6.org/dist/StrictNamedArguments适合你。

© www.soinside.com 2019 - 2024. All rights reserved.