【问题标题】:Are private attributes hidden by default by .perl and .gist默认情况下,私有属性是否被 .perl 和 .gist 隐藏
【发布时间】:2019-02-26 13:29:44
【问题描述】:

好像是这样的:

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

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

【问题讨论】:

    标签: oop raku


    【解决方案1】:

    .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(又名bar => 3)被静默忽略,因为没有名为bar没有公共属性。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2018-05-07
    • 2020-01-12
    • 2021-08-04
    相关资源
    最近更新 更多