How to define a custom type of an Array-Reference of Ints in Perl 6? I tried this, but it doesn't work:
subset Array_of_Int of Array where *.all ~~ Int;
my $n = My::Class.new( option => < 22 3 4 5 > );
# Type check failed in assignment to $!option; expected My::Class::Array_of_Int but got List in block <unit> at ...
In My::Class:
has Int @.option;
I'm not sure why to do this, most perl6 programmers with declare subset for array element but not for array itself. Rakudo decides to create List
instead of Array
-> the same trap comes when using Rat
type instead of Num
. Anyway it is possible. Subset is not fully qualified type (it is not possible to instance it). You have to create an array explicitly $aoi = Array[Int].new(1,2,3,4,5,6)
.
> subset AoI of Array of Int
> my AoI $aoi;
> $aoi = Array[Int].new
> $aoi.append(1,2,3,4)
[1 2 3 4]
> $aoi.append("mystr")
Type check failed in assignment to ; expected Int but got Str
in block <unit> at <unknown file> line 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With