[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: ruby1.9 block scope

Yukihiro Matsumoto

9/29/2008 12:42:00 PM

Hi,

In message "Re: ruby1.9 block scope"
on Mon, 29 Sep 2008 21:24:02 +0900, Daniel DeLorme <dan-ml@dan42.com> writes:

|> | >> 1.times{ x=2 }
|> |=> 1
|> | >> x
|> |NameError: undefined local variable or method `x' for main:Object

|> I have once presented a plan to make it work so, but have never
|> implemented. So it's not "changed reversed". Just the idea
|> abandoned.
|
|Ah, ok, then I guess I *did* dream the whole thing. For the record, I
|think it's a great idea and deserves to be implemented... in ruby 2.0
|maybe ;-)

My vague idea for the issue is that when you see the reference to a
local variable inside of the block, the scope of the variable would be
upgraded to the level of the reference.

matz.

7 Answers

Daniel DeLorme

9/29/2008 3:23:00 PM

0

Yukihiro Matsumoto wrote:
> |> I have once presented a plan to make it work so, but have never
> |> implemented. So it's not "changed reversed". Just the idea
> |> abandoned.
> |
> |Ah, ok, then I guess I *did* dream the whole thing. For the record, I
> |think it's a great idea and deserves to be implemented... in ruby 2.0
> |maybe ;-)
>
> My vague idea for the issue is that when you see the reference to a
> local variable inside of the block, the scope of the variable would be
> upgraded to the level of the reference.

Do you mean upgrade the scope of the variable dynamically? Is that even
possible? I thought that all local vars had their scope pretty much set
in stone at the lexical level. But if you could upgrade their scoping
level dynamically... that would be nifty indeed!

--
Daniel

Lex Williams

9/29/2008 5:02:00 PM

0

Daniel DeLorme wrote:
> Yukihiro Matsumoto wrote:
>> upgraded to the level of the reference.
> Do you mean upgrade the scope of the variable dynamically? Is that even
> possible? I thought that all local vars had their scope pretty much set
> in stone at the lexical level. But if you could upgrade their scoping
> level dynamically... that would be nifty indeed!

If he did that , we wouldn't have locals anymore .
--
Posted via http://www.ruby-....

Nobuyoshi Nakada

9/29/2008 7:06:00 PM

0

Hi,

At Tue, 30 Sep 2008 00:23:17 +0900,
Daniel DeLorme wrote in [ruby-talk:316347]:
> > My vague idea for the issue is that when you see the reference to a
> > local variable inside of the block, the scope of the variable would be
> > upgraded to the level of the reference.
>
> Do you mean upgrade the scope of the variable dynamically? Is that even
> possible? I thought that all local vars had their scope pretty much set
> in stone at the lexical level. But if you could upgrade their scoping
> level dynamically... that would be nifty indeed!

I guess the parser would do it at the compile time.

--
Nobu Nakada

namekuseijin

9/29/2008 7:59:00 PM

0

On Sep 29, 12:23 pm, Daniel DeLorme <dan...@dan42.com> wrote:
> Yukihiro Matsumoto wrote:
> > |> I have once presented a plan to make it work so, but have never
> > |> implemented.  So it's not "changed reversed".  Just the idea
> > |> abandoned.
> > |
> > |Ah, ok, then I guess I *did* dream the whole thing. For the record, I
> > |think it's a great idea and deserves to be implemented... in ruby 2.0
> > |maybe ;-)
>
> > My vague idea for the issue is that when you see the reference to a
> > local variable inside of the block, the scope of the variable would be
> > upgraded to the level of the reference.
>
> Do you mean upgrade the scope of the variable dynamically? Is that even
> possible? I thought that all local vars had their scope pretty much set
> in stone at the lexical level. But if you could upgrade their scoping
> level dynamically... that would be nifty indeed!

Yeah, that would be so... 1970's!

Even javascript seems to finally have abandoned dynamic scoping in
favor of friggin' *ALGOL*'s lexical scoping... I wonder why would ruby
insist on sloppy programming by opening this old can of worms...

Daniel DeLorme

9/30/2008 12:11:00 AM

0

Nobuyoshi Nakada wrote:
>
> At Tue, 30 Sep 2008 00:23:17 +0900,
> Daniel DeLorme wrote in [ruby-talk:316347]:
>>> My vague idea for the issue is that when you see the reference to a
>>> local variable inside of the block, the scope of the variable would be
>>> upgraded to the level of the reference.
>> Do you mean upgrade the scope of the variable dynamically? Is that even
>> possible? I thought that all local vars had their scope pretty much set
>> in stone at the lexical level. But if you could upgrade their scoping
>> level dynamically... that would be nifty indeed!
>
> I guess the parser would do it at the compile time.

That was also my guess; it least it's an easy concept to imagine. But it
does bring the question of what to do when you *don't* want variables to
"escape" into the enclosing scope? e.g.

class Foo
defined_method :bar do
x = 1 #I don't want x to be defined in the Foo scope
end
defined_method :baz do
x = 2 #because it would wind up shared with this method
end
end

The best I could think of was that a variable shouldn't be upgraded if
the enclosing scope is a "class" or "module" section. That can be
detected at parse time so it would work with lexical binding.

But it would be very interesting indeed if you could bind a block's
variables to the enclosing scope dynamically. You could say:
block.call_using_enclosing_scope #e.g. define_method
block.call_using_own_scope #e.g. each

To me that feels very very powerful. And more power to the programmer is
good, right? ;-)

--
Daniel

Daniel DeLorme

9/30/2008 12:14:00 AM

0

namekuseijin wrote:
> On Sep 29, 12:23 pm, Daniel DeLorme <dan...@dan42.com> wrote:
>> Yukihiro Matsumoto wrote:
>>> My vague idea for the issue is that when you see the reference to a
>>> local variable inside of the block, the scope of the variable would be
>>> upgraded to the level of the reference.
>> Do you mean upgrade the scope of the variable dynamically? Is that even
>> possible? I thought that all local vars had their scope pretty much set
>> in stone at the lexical level. But if you could upgrade their scoping
>> level dynamically... that would be nifty indeed!
>
> Yeah, that would be so... 1970's!
>
> Even javascript seems to finally have abandoned dynamic scoping in
> favor of friggin' *ALGOL*'s lexical scoping... I wonder why would ruby
> insist on sloppy programming by opening this old can of worms...

Hehe, you have somewhat of a point. Local variables and proper isolation
of scope are pretty important to structured programming. But we
weren't talking about a free-for-all opening of all scopes, just about
the scope of local variables within *blocks*. I wouldn't much like my
method-local variables to creep into the global scope just because I
forgot to define them with "var", no thank you.

--
Daniel

Nobuyoshi Nakada

9/30/2008 12:45:00 AM

0

Hi,

At Tue, 30 Sep 2008 09:10:39 +0900,
Daniel DeLorme wrote in [ruby-talk:316386]:
> class Foo
define_method :bar do |;x|
> x = 1 #I don't want x to be defined in the Foo scope
> end
define_method :baz do |;x|
> x = 2 #because it would wind up shared with this method
> end
> end

--
Nobu Nakada