No, it is not possible to override an attribute, but you can initialize it using the subtext of the BUILD class:
role A { has $.a; } class B does A { submethod BUILD(:$!a = 'default') {} }
Note that if you just set the value in the body of the BUILD instead of your signature via
class B does A { submethod BUILD { $!a = 'default' } }
the user cannot overwrite the default value by providing a named initializer through B.new(a => 42) .
Another, more elegant approach, which is especially useful when installing by default, is that you expect to do a lot (that is, the default value can be considered as part of the role interface) makes it a parameter:
role A[$d] { has $.a = $d; } class B does A['default'] {}
source share