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

p6rt opened this issue Sep 26, 2015 · 2 comments


Copy link

@p6rt p6rt commented Sep 26, 2015

Migrated from (status was 'new')

Searchable as RT126198$

Copy link

@p6rt 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 {
  # 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 =​:constant(6), :varies(6));
say $mv.varies.VAR.^name; # Proxy
say $mv.varies.^name; # Method

<moritz> m​:
<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

Copy link

@p6rt 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
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant