从“执行"位置的类中的 AT-POS 方法(而不是代理实例)返回“原始"标量容器? [英] Returning a 'raw' scalar container from AT-POS method (rather than a Proxy instance) in a class that 'does' Positional?
问题描述
我正在尝试实现一个执行"Positional
的类,该类还允许我通过分配给 AT-POS
方法返回的结果来更新其值.最终,我能够编造以下按预期工作的类:
I'm attempting to implement a class that 'does' Positional
that also allows me to update its values by assigning to the result returned by the AT-POS
method. Eventually, I was able to concoct the following class that works as intended:
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] == 12;
$test[7] = 120;
die unless $test[2] == 24;
die unless $test[5] == 120;
$test[10] = 240;
die unless $test[2] == 240;
die unless $test[5] == 120;
是否有可能以某种方式(并且:简单地)返回绑定到 $!slot_1
(或 $!slot_2
)的 containerTest
类的实现?
Would it be possible to somehow (and: simply) return the container bound to $!slot_1
(or $!slot_2
) inside the Test
class implementation?
在我发现使用 Proxy
实例之前,我尝试 return
(和 return-rw
)表达式 position 的结果% 2 ??$!slot_1.VAR !!$!slot_2.VAR
,因为我的印象是 VAR
方法让我可以访问底层容器,希望我可以简单地return
它.这并没有真正起作用,我还不明白为什么:我怀疑它以某种方式被强制恢复为某个值?
Before I discovered the use of Proxy
instances I attempted to return
(and return-rw
) the result of expression position % 2 ?? $!slot_1.VAR !! $!slot_2.VAR
, because I'm under the impression that the VAR
method gives me access to the underlying container, in the hope that I can simply return
it. That didn't really work, and I do not understand why yet: I suspect it somehow gets coerced back to a value somehow?
换句话说:在这种特殊情况下,是否可以简化我的 AT-POS
实现?
So in other words: is it possible to simplify my AT-POS
implementation in this particular situation?
谢谢,
问候,
雷蒙德.
推荐答案
假设您不想要slot_1"的访问器和slot_2",如果我正确理解了这个问题,这将是我的实现.我不会称它为 Test
类,因为这会干扰用于测试的 Test
类.
Assuming you do not want accessors for "slot_1" and "slot_2", and if I understand the question correctly, this would be my implementation. I wouldn't call it a Test
class, as that would interfere with the Test
class that is used for testing.
class Foo {
has @elements = 24, 12;
method AT-POS(Int:D $pos) is raw {
@elements[$pos % 2]
}
}
my $f = Foo.new;
say $f[2]; # 24
say $f[5]; # 12
$f[2] = 666;
say $f[4]; # 666
注意数组中的默认值已经改变了顺序,这是为了保持AT-POS
中的算术简单.
Note that the defaults in the array have changed order, that's to keep the arithmetic in AT-POS
simple.
还要注意定义中的is raw
AT-POS
方法:它将确保在返回值时不会发生去容器化.这允许您只分配给 $f[2]
返回的任何内容.
Also note the is raw
in the definition of the AT-POS
method: it will ensure that no de-containerization will take place when returning a value. This allows you to just assign to whatever $f[2]
returns.
希望这是有道理的!
另外:Array::Agnostic 模块可能对您感兴趣,直接使用,或用作灵感来源.
Also: the Array::Agnostic module may be of interest for you, to use directly, or to use as a source of inspiration.
这篇关于从“执行"位置的类中的 AT-POS 方法(而不是代理实例)返回“原始"标量容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!