支持Whatever ranges in multidimensional subscript access with AT-POS

Support Whatever ranges in multidimensional subscript access with AT-POS

如何实现 AT-POS 以使其支持多维 Whatever 范围,例如 [0;*][*;0]

在下面的实现中,我得到 Index out of range 个错误:

class Foo {
    has @.grid;
    multi method elems { @!grid.elems }
    multi method AT-POS($y, $x) is rw { @!grid[ $y ; $x ] }
    multi method ASSIGN-POS ($y, $x, $new) { @!grid[ $y; $x ] = $new }
    multi method EXISTS-POS($y, $x) { @!grid[ $y; $x ]:exists }
}

my $foo = Foo.new: :grid[ ['a'], ['b', 'c'] ];
say $foo[0;0];         # a
say $foo[0;*].elems;   # Expect 1, get 2
say $foo[0;*];         # Expect (a), get "Index out of range"
say $foo[*;0];         # Expect (a b), get "Index out of range"

The doc 表示 API 是 AT-POS($index)

当我将你的 AT-POS 替换为:

    multi method AT-POS($index) is rw { @!grid[ $index ] }

您的测试用例给出了您期望的结果。

你的 ASSIGN-POS 是不必要的,可能会出错。