I'm trying to find how the binding operation works on attributes and what makes it so different from nqp::bindattr
. Consider the following example:
class Foo {
has @!foo;
submethod TWEAK {
my $fval = [<a b c>];
use nqp;
nqp::bindattr( nqp::decont(self), $?CLASS, '@!foo',
#@!foo :=
Proxy.new(
FETCH => -> $ { $fval },
STORE => -> $, $v { $fval = $v }
)
);
}
method check {
say @!foo.perl;
}
}
my $inst = Foo.new;
$inst.check;
It prints:
$["a", "b", "c"]
Replacing nqp::bindattr
with the binding operator from the comment gives correct output:
["a", "b", "c"]
Similarly, if foo
is a public attribute and accessor is used the output would be correct too due to deconterisation taking place within the accessor.
I use similar code in my AttrX::Mooish
module where use of :=
would overcomplicate the implementation. So far, nqp::bindattr
did the good job for me until the above problem arised.
I tried tracing down Rakudo's internals looking for :=
implementation but without any success so far. I would ask here either for an advise as to how to simulate the operator or where in the source to look for its implementation.
--target
povides more clear view. Note also that my knowelge of compiler internals is very basic and I might be asking silly questions. But for now I have discovered that:=
doesn't work the way I thought it would. It deconts instantly thus binding not to aProxy
instance but to whatFETCH
returns at the moment when binding happens. Of course, same happens with decont-wrappedProxy
asbindattr
parameter. – Eveevection