[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Global by default
- From: steve donovan <steve.j.donovan@...>
- Date: Wed, 13 Feb 2013 08:48:56 +0200
On Tue, Feb 12, 2013 at 9:57 PM, David Olofson <david@olofson.net> wrote:
> TL;DR: Implicit behavior is dangerous.
For example, consider how the Lua-based language Moonscript handles
this. All declarations are local.
if a > 0
x = 1
print x
x is local to the block, so it always prints out nil! You have to put
an explicit 'x = nil' or 'local x' before the if to force the
declaration.
Functions may optionally declare exactly what upvalues they _are_
using. Here it's obvious enough from the context, but this is a
useful way to annotate the behaviour explicitly in more complicated
situations.
counter = 1
add = (x using counter) ->
counter = counter + 1
add 1
print counter
http://moonscript.org/reference/#the_using_clause_controlling_destructive_assignment
Furthermore, you need a new statement (export) to ensure that
something _does_ become global, and so forth.
MS is not Lua, (altho generates pretty clear Lua as output), but you
see the extra complexity needed to preserve 'local by default'
steve d.