From: Austin Ziegler Date: 2005-10-19T12:26:08+09:00 Subject: Re: Anonymous methods, blocks etc. (Cont. 'default block params') On 10/18/05, Trans wrote: > Austin Ziegler wrote: >> On 10/18/05, Sean O'Halpin wrote: >>> On 10/18/05, Eric Mahurin wrote: >>>> But, I kind of still wish we had a syntax where the variables in a >>>> block/lambda were local (non-closure) by default. >>> Maybe if we say it often enough? ;) >> Doubtful. I think to convince matz, real examples would need to be >> presented (as an RCR!) as to why this is necessary. IANM, but IMO >> theoretical need and wish ain't sufficient. > The easist is the old gotcha. Programmer has code (doesn't much matter > what it is) [...] > And wants to resue the lambda so uses old fashion copy and paste: > > class Y > def bar( n ) > x = n * 3 > l = lambda { |a| x = a**2; x + 1 } > return x + l[3] > end > end > > Oopsy. Dumb programmer. This part isn't nearly sufficient to justify a new scope or kind of lambda (lambda without closure), IMO. If you want to reuse a lambda that you're not passing around, make it a method. > For something a little more concrete: [...] > Now try: > > class X > def x;1;end > end > => nil > > Y = Class.new(X) do > X = Class.new do > def x;2;end > end > def x;3;end > end > (irb):5: warning: already initialized constant X > => Y > > X.new.x > => 2 > Y.new.x > => 3 > Y::X.new.x > (irb):12: warning: toplevel constant X referenced by Y::X > => 2 This isn't a closure problem. This is a constant lookup problem, and Matz has said that this should be fixed moving forward. As I said, I'm very skeptical. -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca