[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Redefining locals
- From: Matt Hellige <matt@...>
- Date: Fri, 12 Nov 2004 10:27:21 -0600
[David Given <dg@cowlark.com>]
> On Thursday 11 November 2004 21:22, Matt Hellige wrote:
> [...]
> > I think it would be nice for lua to adopt a similar policy for locals,
> > as has been suggested by others. I'd like:
> >
> > local x = 3
> > function f() print(x) end
> > f()
> > local x = 5
> > f()
> >
> > to print '3, 5' instead of '3, 3'...
>
> This strikes me as being counterintuitive. I expect the explicit use of local
> to define a *new* variable. f() is defined using the old version of x as an
> upvalue, so defining a new one would simply hide the old one from any further
> code.
>
> I don't know Scheme, but I do know ML, and I'd expect local to work like 'let
> <var> = <value> in...' (except with mutable variables, of course).
>
But in ML, let explicitly and syntactically introduces a new scope,
hence the "in ..." clause. If you want that with Lua, I think you
should use "do ... end", which syntactically and semantically more
closely matches the 'let' expression in ML. I guess I just prefer
scopes to be syntactically obvious and explicit. I don't really feel
that strongly about this issue, though, so I should probably defer to
popular opinion.
Matt
--
Matt Hellige matt@immute.net