Hello,
matz@ruby-lang.org (Yukihiro Matsumoto) wrote in message
news:1023845919.929356.32025.nullmailer@picachu.netlab.jp…
(c-1) local variables assigned by “:=” are in-block variables,
otherwise they are method local variables. If you assigns to
an existing local variable, the variable is shadowed, and you
will probably be warned.
Matz, what about making this distinction more general than just
block-local vs. method local? That is, always be able to use := to
create a new binding rather than modifying an existing one. So, if
you had
def foo
x := 1
y := 2
myproc = lambda{p x, y}
x := 10
y = 20
myproc.call
end
#=> 1
#=> 20
Bindings would be scoped to thier closest enclosing method or block;
using = when no binding already existed for the variable would be the
same as using :=.
My main motivation for wanting this is to ease state management when
using continuations for backtracking - creating new bindings is
obviously safer here than mutating existing ones.
Avi
I agree about the relevance of that issue.
There is something I don’t understand however: aren’t you creating
a huge number of new bindings with := ? Specially considering the
fact that it is so easy to type := instead of =. Some people may
even prefer using := instead of =, overlooking some important
differences, just because := looks more like an assignment than a
comparison for equality.
def foo( stream, l )
x, msg, t = nil, nil, nil
for x in l do
# Using your generalized :=
msg := x.to_s()
t := Time.now
bar( stream, msg) do
stream << msg if (Time.now - t) <= 1
end
# versus today (with same apparent result):
msg = x.to_s()
t = Time.now
begin
local_msg = msg
local_t = t
bar( stream, msg) do
stream << msg if (Time.now - t) <= 1
end
end
# versus my own proposal: unbound
msg = x.to_s()
t = Time.now
bar( stream, msg) do
unbound msg, t # <===
stream << msg if (Time.now - t) <= 1
end
end
end
With my explicit “unbound” statement, the author is more explicit about
the fact that he/she wants some variables to be local to the block, and
that she/he also want these variables to be initialized with the value
of the same variable in the enclosing scope. The total number of bindings
would be the same as today I guess (or similar).
Nota: ‘do local x; xxx end’ would be similar to ‘do unbound x; xxx end’,
the diff is that with “local” x is initialized when the block is called,
versus when the block is created.
I understand that Ruby does not require explicit declaration of variables
and that this is considered a plus. However, “simple things should be
simple and complex ones possible”, and I feel like the need for “truly
local block param/var” and “initialized at block creation time local
block variables” signals that something complex/tricky is occurring…
well… just a feeling.
Yours,
Jean-Hugues
···
At 10:43 13/06/2002 +0900, you wrote:
Web: http://hdl.handle.net/1030.37/1.1
Phone: +33 (0) 4 92 27 74 17