There is an example:
class A is Array {
has $.member = 42;
}
my $a = A.new: 1..10;
say $a.member; # OUTPUT: «(Any)»
Why doesn't it work? I also tried submethods TWEAK
and BUILD
, they also can't set a default value.
I found this workaround:
class A is Array {
has $.member is rw;
method new(|) {
my $instance = callsame;
$instance.member = 42;
return $instance;
}
}
my $a = A.new: 1..10;
say $a.member; # OUTPUT: «42»
Some core classes are currently not really set up to handle subclasses. Array
being one of them. You've found one workaround. Another workaround (if you only need 1 additional attribute) is to use a role
and does
.
role Member {
has $.member is rw = 42
}
my @a does Member = 1..10;
say @a.member; # 42
my @b does Member(666) = 1..10;
say @b.member; # 666
This should probably be made an issue