小编chr*_*mis的帖子

从“执行”位置的类中的 AT-POS 方法(而不是代理实例)返回“原始”标量容器?

我正在尝试实现一个“做”的类,Positional它也允许我通过分配给AT-POS方法返回的结果来更新它的值。最终,我能够编造以下按预期工作的类:

class Test does Positional
{
    has $.slot_1 is rw = 12;
    has $.slot_2 is rw = 24;

    method AT-POS(\position)
    {
        my $t = self;

        return-rw Proxy.new:

            FETCH => method ()
            {
                position % 2 ?? $t.slot_1 !! $t.slot_2
            },

            STORE => method ($v)
            {
                if position % 2
                {
                    $t.slot_1 = $v
                }
                else
                {
                    $t.slot_2 = $v
                }
            }
    }
}

my $test = Test.new;

die unless $test[2] == 24;

die unless $test[5] == …
Run Code Online (Sandbox Code Playgroud)

raku

8
推荐指数
1
解决办法
96
查看次数

标签 统计

raku ×1