覆盖角色的属性

Override Attribute of a Role

是否可以覆盖角色的属性以提供默认值?

role A {
     has $.a;
}
class B does A {
    has $.a = "default";
}
my $b = B.new;

这会导致编译错误:

===SORRY!=== Error while compiling:
Attribute '$!a' already exists in the class 'B', but a role also wishes to compose it

由于 R 中的方法可能会引用 $!a,因此引用的属性可能不明确。

使用子方法 BUILD 初始化 inherited/mixedin 个属性。

role R { has $.a };
class C does R {
    submethod BUILD { $!a = "default" }
};
my $c = C.new;
dd $c;
# OUTPUT«C $c = C.new(a => "default")␤»

根据您的用例,您最好通过角色参数设置默认值。

role R[$d] { has $.a = $d };
class C does R["default"] { };
my $c = C.new;
dd $c;
# OUTPUT«C $c = C.new(a => "default")␤»

不,不可能重新声明属性 - 但您可以使用 class:

BUILD 子方法对其进行初始化
role A {
     has $.a;
}

class B does A {
    submethod BUILD(:$!a = 'default') {}
}

请注意,如果您只是在 BUILD 的正文中设置值,而不是通过

设置其签名
class B does A {
    submethod BUILD { $!a = 'default' }
}

用户将无法通过 B.new(a => 42).

提供命名初始化程序来覆盖默认值

另一种更优雅的方法特别有用,如果设置默认值是您希望做的很多事情(即默认值可以被视为角色接口的一部分),那就是将其作为参数:

role A[$d] {
    has $.a = $d;
}

class B does A['default'] {}