From: Tom Sawyer Date: 2003-02-01T22:57:42+09:00 Subject: Re: Local variables & blocks On Saturday 01 February 2003 05:59 am, Brian Candler wrote: > > (guess no one read my other message on this) > > I did. I'm just not really qualified to comment. But I will anyway :-) sure you are. i really just needed some feed back to stir my own brain cells. thanks. > It always seemed strange to me that 'local variables' were not really part > of an object, and therefore seemed to break the OO model. I think what you > are proposing is to turn them into a special sort of instance variable: so > > x = 1 > > is a bit like > > @x = 1 indeed, that's exactly what bugs me too. > except that the second case persists with the object for its entire > lifetime, whereas the first case persists only until the end of, erm, I'm > not quite sure - the current lexical scope? yes the scope > Perhaps then a new lexical > scope create a temporary subclass where these instance variables are > deposited? All method calls (including instances of local variables) will > then follow the normal chain of calling back up to the parent. hmmm..now that gives me a notion.... > I'm not sure how this differs from the current situation though, if you > replace "temporary subclass" with "stack frame". > > I suppose it might make local variables more widely available - for > example: > > class Foo > def bar > puts i > end > def baz > i = 10 > bar > end > end > > Foo.new.baz # doesn't work now > > would allow the value of 'i' to be seen in 'bar' when called from 'baz'. If > that were desirable though, I'd have thought the current implementation > could allow it (e.g. by searching through all the stack frames, not just > the current one) essentially if it were seen by bar defined by baz then it would be pretty much the same as @i. hey, perhaps we don't need no stink'en locals ;-) i realize now that self can't really define the scope. b/c then any definition would actually bind self to it . i.e. class Foo def bar self #--> normally instance of Foo, but scope is Foo#bar end end perhaps having an attribute of the object actually called scope would be of interest? class Foo def bar self #--> instance of Foo scope #--> instance of Foo's bar method end end thus local variables such as: x = 1 are in effect: scope.x = 1 Thanks, tom sawyer, aka transami transami@transami.net