I'm trying to understand why an attribute value is a Str (or whatever) outside of Proxy.new, but becomes Method inside of Proxy.new. I golfed my code down to this:
#!/usr/bin/env raku
class Foo does Associative {
has Str $.string;
multi method AT-KEY (::?CLASS:D: Str $key) is rw {
say "\nOutside of Proxy: { $!string.raku }";
say "Outside of Proxy: { $!string.^name }";
Proxy.new(
FETCH => method () {
say "FETCH: { $!string.raku }";
say "FETCH: { $!string.^name }";
},
STORE => method ($value) {
say "STORE: { $!string.raku }";
say "STORE: { $!string.^name }";
}
);
}
}
my $string = 'foobar';
my %foo := Foo.new: :$string;
%foo<bar> = 'baz';
say %foo<bar>;
This is the output:
$ ./proxy.raku
Outside of Proxy: "foobar"
Outside of Proxy: Str
STORE: method <anon> (Mu: *%_) { #`(Method|94229743999472) ... }
STORE: Method
Outside of Proxy: "foobar"
Outside of Proxy: Str
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
True
Can anyone explain what's going on here? Thanks!
The problem is that $!string
is an empty container by the time the block is actually called. Even if it's declared in scope, by the time it's actually used $!str
is really pointing to an anonymous method. Just add say $!string
in your FETCH
or STORE
and you'll see that it contains an anonymous Block
, printing <anon>
.
If what you want is to work on attributes, check this other SO answer to see how it could be done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With