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

has initializers should be methods with 'self' #968

Closed
p6rt opened this issue May 3, 2009 · 7 comments
Closed

has initializers should be methods with 'self' #968

p6rt opened this issue May 3, 2009 · 7 comments
Labels

Comments

@p6rt
Copy link

p6rt commented May 3, 2009

Migrated from rt.perl.org#65346 (status was 'resolved')

Searchable as RT65346$

@p6rt
Copy link
Author

p6rt commented May 3, 2009

From jswitzer@gmail.com

I ran into a bug where I couldn't refer to 'self' when it logically made
sense I could​:

(15​:33​:15) s1n​: rakudo​: class A { has %.H = (a => -> $x { self.c($x) });
method c($c) { say "got $c" }; method test { %.H<a>.("foo").say; }; };
A.new().test();
(15​:33​:20) p6eval​: rakudo cddb16​: OUTPUT«Lexical 'self' not found␤current
instr.​: 'parrot;A;_block27' pc 243 (EVAL_24​:111)␤»
(15​:33​:54) masak​: s1n​: in fact, 'self' is not defined at the place you're
using it.
(15​:34​:04) masak​: s1n​: only use it from within methods.
(15​:34​:36) moritz_​: but why? `self' in class should just refer to the class
(formerly proto object), no?
(15​:34​:47) masak​: hm.
(15​:34​:51) s1n​: it exists, it should
(15​:34​:56) masak​: rakudo​: class A { self }
(15​:35​:00) p6eval​: rakudo cddb16​: OUTPUT«Lexical 'self' not found␤current
instr.​: 'parrot;A;_block20' pc 94 (EVAL_18​:66)␤»

I am sure there are far simpler test cases. jnth++ agrees that people may be
burned more often than not with the current behavior of self.

-Jason "s1n" Switzer

@p6rt
Copy link
Author

p6rt commented May 4, 2009

From @pmichaud

On Sun May 03 13​:51​:11 2009, s1n wrote​:

I ran into a bug where I couldn't refer to 'self' when it logically made
sense I could​:

(15​:33​:15) s1n​: rakudo​: class A { has %.H = (a => -> $x { self.c($x) });

I don't see anything in the spec that indicates that the body of the
class declaration has a lexical 'self', or that it should be pre-set to
the type object (formerly protoobject). AFAICT 'self' is only defined
for method bodies, and it's the invocant of the method.

If that's not the case, we probably need a spec update. In the
meantime, I'm changing the title of this ticket to more accurately
reflect the question at hand.

Also, S12 indicates that C<self> is a function, whereas we currently
implement it as a lexical. If it needs to be a function, we can
probably arrange that. (And perhaps it's a contextual lookup of some sort.)

Thanks,

Pm

@p6rt
Copy link
Author

p6rt commented May 4, 2009

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented May 4, 2009

From @pmichaud

21​:00 <TimToady> pmichaud​: note that for the self ticket, the phrase in
question is a has initializer, which runs in BUILD context, and hence
does have an obvious instance self, so this isn't really about binding
self to the class
21​:04 <pmichaud> TimToady​: okay, thanks.
21​:04 <pmichaud> So we definitely can't leave self as being a plain
lexical, unless we're making use of 'lift' somehow.
21​:05 <pmichaud> or otherwise attach the context to self... which we
have to do anyway. I'll have to think about it a bit more.
21​:05 <jnthn> The thing on the RHS of the = is made a closure already;
we just need to make it an anonymous method.
21​:05 <pmichaud> That works for me.
21​:05 <jnthn> (and remember to pass self in too)
21​:06 <pmichaud> well, the closure has to be invoked on the newly built
object, I suspect.
21​:06 <jnthn> yes
21​:07 <pmichaud> okay, I'll update the ticket.

@p6rt
Copy link
Author

p6rt commented May 4, 2009

From @pmichaud

21​:08 <pmichaud> in the general case, though, is 'self' available in the
body of a class declaration?
21​:08 <pmichaud> (as opposed to within initializers, as the ticket case
shows)
21​:08 <jnthn> No.
21​:08 <pmichaud> (ticket updated)
21​:08 <TimToady> I don't see what it buys us that $?CLASS does give us
better
21​:09 <TimToady> or some reasonable fascimile
21​:09 <TimToady> self in the class seems to be confusion waiting to happen
21​:09 <pmichaud> good for me
21​:09 <pmichaud> more ticket updating. :-)

@p6rt
Copy link
Author

p6rt commented May 20, 2009

From @jnthn

On Mon May 04 14​:07​:56 2009, pmichaud wrote​:

21​:00 <TimToady> pmichaud​: note that for the self ticket, the phrase in
question is a has initializer, which runs in BUILD context, and hence
does have an obvious instance self, so this isn't really about binding
self to the class
21​:04 <pmichaud> TimToady​: okay, thanks.
21​:04 <pmichaud> So we definitely can't leave self as being a plain
lexical, unless we're making use of 'lift' somehow.
21​:05 <pmichaud> or otherwise attach the context to self... which we
have to do anyway. I'll have to think about it a bit more.
21​:05 <jnthn> The thing on the RHS of the = is made a closure already;
we just need to make it an anonymous method.
21​:05 <pmichaud> That works for me.
21​:05 <jnthn> (and remember to pass self in too)
21​:06 <pmichaud> well, the closure has to be invoked on the newly built
object, I suspect.
21​:06 <jnthn> yes
21​:07 <pmichaud> okay, I'll update the ticket.

Updated Rakudo to work this way in git 6229131. So now these work​:

class Foo { has $.a = 1; has $.b = 2; has $.c = $.a + $.b;}
say Foo.new.c
3

And​:

class Bar { has $.r = rand }
say Bar.new.r for 1..3;
0.470490443377884
0.6400637382559
0.878367274158286

Did some unfudging and additions to S12-attributes/instance.t.

Thanks,

Jonathan

@p6rt
Copy link
Author

p6rt commented May 20, 2009

@jnthn - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed May 20, 2009
@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant