Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute Value Becomes "Method" Inside Proxy.new (Raku)

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!

like image 479
JustThisGuy Avatar asked Dec 18 '20 16:12

JustThisGuy


1 Answers

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.

like image 97
jjmerelo Avatar answered Oct 03 '22 14:10

jjmerelo