How to define a custom Arrayreference Ints type in Perl 6?

How to define a custom reference array type for Ints in Perl 6? I tried this, but it does not 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 ... 
+5
source share
2 answers

In My :: Class:

 has Int @.option; 
+5
source

I'm not sure why this is necessary, most perl6 programmers declare a subset for an array element, but not for the array itself. Rakudo decides to create a List instead of Array the same trap appears when using the Rat type instead of Num . In any case, it is possible. The subset is not fully qualified (it is not possible). You must explicitly create the array $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 
+2
source

Source: https://habr.com/ru/post/1239885/


All Articles