Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining a Proxy with methods instead of subs leads to surprising results in Rakudo #4594

Open
p6rt opened this issue Sep 26, 2015 · 2 comments

Comments

@p6rt
Copy link

p6rt commented Sep 26, 2015

Migrated from rt.perl.org#126198 (status was 'new')

Searchable as RT126198$

@p6rt
Copy link
Author

p6rt commented Sep 26, 2015

From @masak

moritz++' gist inlined for your convenience​:

our $count = 0;

class MagicVal {
  has Int $.constant;
  has Int $.varies = 0;

  method varies is rw {
  $count++;
  return-rw Proxy.new(
  # note that FETCH and STORE cannot go through the accessors
  # of $.varies again, because that would lead to infinite
  # recursion. Use the actual attribute here instead
  FETCH => method () { $!varies },
  STORE => method ($new) { $!varies = $new + 1 },
  );
  }
}

my $mv = MagicVal.new(​:constant(6), :varies(6));
say $mv.varies.VAR.^name; # Proxy
say $mv.varies.^name; # Method

<moritz> m​: https://gist.github.com/moritz/5cda6371ab3cfec3254e
<camelia> rakudo-moar 0132b6​: OUTPUT«Proxy␤Method␤»
<moritz> why does the last line print Method here?
<moritz> and not Int?
<masak> good question.
<masak> does just `$mv.varies` print 6?
<moritz> masak​: no, it says <anon>
<masak> sounds wrong to me.
<ShimmerFairy> moritz​: Well, it's because you mapped those keys to
Method objects, no?
<masak> ShimmerFairy​: isn't that the way you implement a Proxy, though?
<masak> you give FETCH and STORE a callable each. doesn't mean they get exposed.
<ShimmerFairy> masak​: I've never seen the method keyword used with it,
I don't think. Just plain closures
<masak> ShimmerFairy​: the presence of the `method` keyword there is
practically irrelevant.
<moritz> ShimmerFairy​: the 'method' keyword just means they get an invocant
<masak> m​: my &c1 = sub ($obj, $x) { say "$obj​: $x" }; my &c2 = method
($obj​: $x) { say "$obj​: $x" }; c1("OH HAI", 42); "OH HAI".&c2(42)
<camelia> rakudo-moar 0132b6​: OUTPUT«OH HAI​: 42␤OH HAI​: 42␤»
<masak> ShimmerFairy​: ^
<masak> it's only a matter of intent, not a matter of differing semantics.
<moritz> huh
<moritz> but changing it to a sub instead helps indeed
<moritz> wtf?
<masak> ok, bug
* masak submits rakudobug
<masak> it's probably special-cased where it oughtn't be
<jdv79> i think i ran into that a few months ago and just ignored it
when someone showed me the working one
<masak> let's fix it instead :P

@p6rt
Copy link
Author

p6rt commented Sep 26, 2015

From @moritz

FWIW, fudged tests are in S12-attributes/mutators.t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant