如何在 Perl 6 中定义 Ints 的 Arrayreference 的自定义类型?

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

如何在 Perl 6 中定义自定义类型的整数数组引用?我试过这个,但它不起作用:

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 ...

我不确定为什么要这样做,大多数 perl6 程序员都声明数组元素的子集而不是数组本身。 Rakudo 决定创建 List 而不是 Array -> 使用 Rat 类型而不是 Num 时会出现相同的陷阱。无论如何这是可能的。子集不是完全限定的类型(不可能实例化它)。您必须明确地创建一个数组 $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

在My::Class中:

has Int @.option;