Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ...
like image 531
sid_com Avatar asked Jan 05 '16 13:01

sid_com


2 Answers

In My::Class:

has Int @.option;
like image 117
CIAvash Avatar answered Nov 15 '22 09:11

CIAvash


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
like image 29
teodozjan Avatar answered Nov 15 '22 08:11

teodozjan