lua-users home
lua-l archive

Another take on locals by default.

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I just stumbled on LiveScript (http://gkz.github.com/LiveScript/), yet
another CoffeeScript fork.
The language has locals by default, and has an interesting solution to
the local/upvalue definition and assignment conundrum.
`a = 5` is always a local assignment. a is declared as local if it
wasn't already. To assingn to an upvalue, you use the := operator.
The language doesn't have globals. Transposed to Lua, this would be.
 make_incr = function()
 acc=0 --local, by default
 return function()
 acc = acc + 1
 -- a local acc shadows the upvalue.
 -- this always add 1 to nil, or 1 to 0,
 -- depending on when you decide to declare the local,
 -- before or after the evaluation of the right hand side.
 return acc
 end
 end
 incr= make_incr()
 incr() --> 1
 incr() --> 1
 -- This works as intended
 make_incr = function()
 acc=0 --local, by default
 return function()
 acc := acc + 1 -- upvalue assignment
 return acc
 end
 end
Marking assignment to non-locals explicit makes IMO the code easier to read.
Globals could use yet another token.
This would prevent a lot of bugs, and make strict.lua obsolete.
If not for Lua, this could be a nice improvement in MoonScript.
Kind regards,
-- Pierre-Yves

AltStyle によって変換されたページ (->オリジナル) /