lua-users home
lua-l archive

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


On Tue, May 12, 2020 at 08:50:03AM -0700, Andrea wrote:
> I am trying to understand the rational for global by default and explicit
> local - I have been reading the lua wiki, lua archive, and all I could find
> on the topic
> 
> I have seen that other languages have had similar discussion, in Python for
> example the proposal is to keep the local by default and use the "nonlocal"
> keyword

The creator of Ruby called local-by-default "the single biggest design flaw
in Ruby". http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/42266

All the problems you have with unintended or accidental bindings are more
severe with default local scoping. And fixing those issues in application
code becomes messier, both syntactically and semantically.

The nice thing about Lua is that because variable binding lookups bubble up
through outer scopes, you can use _ENV to catch them. In that sense there is
no such thing as a "global" variable in Lua, not since _ENV, and arguably
not even in Lua 5.1. Everything is lexical scoped, but there are special
semantics for binding undeclared variables to value at runtime.

Note that some of the quirks in Python and Ruby variable scoping relate to
their origins as strongly object oriented languages, and in particular an
early equivocation of non-global, shared variables with class instance
variables. Lua is a functional languge that permits OOP with some syntactic
sugar and metatable semantics. In modern functional languages closures are
the principle abstraction for data encapsulation, and unless they're "OOP"
they lack the dichotomous categories of static/global and instance
variables; binding variables by recursively searching outer scopes is
natural. When OOP languages like Python and Ruby added lambdas and closures,
things got messier and the best rule wasn't obvious, or it was dicated by
existing implementation constraints. See the Ruby link, above.

It's been nearly 20 years since matz posted his mea culpa, so it's about
time for people to recapitulate the same mistakes. All they know are the
downsides of existing lexical scoping, without any memory of why the
alternative sucks even more.

<snip>
> I take the opportunity to ask another question weakly related to this: I
> have been reading one of the design flaws in Lua is the fact that
> referencing variables that do not exist simply returns nil and no error is
> thrown
> 
> but this seems to be wrong because if it is does not exist, when it is
> referenced the global environment is seached, and access to the global
> environment can be monitored by metamethods, that is strict.lua - am I
> right?

Correct, although it's opt-in rather than opt-out. I think most people would
agree opt-out would be better, but there are other concerns specific to Lua
(e.g. common use of case of sandboxing modules) that make opt-out more
problematic than it might be in other languages.
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org