lua-users home
lua-l archive

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


It was thus said that the Great Coroutines once stated:
> The problem I have with implicitly-local declarations is this:
> 
> In Lua, you can declare a local that shadows something higher up.
> Another local or a global.
> 
> If Lua instead had implicit local declarations, you'd need to declare
> identifiers you'd want to refer to globals in a higher scope.
> 
> Imagine:
> 
> global a, b, c
> a = 3
> 
> You don't know if a is actually a global (in _G) or part of _ENV, or
> just local from a higher scope.  From what discussion I've seen --
> people have only been considering flipping the scoping rules.  If this
> were done, we'd need a few more keywords to precisely identify which
> scope we're connecting identifiers to -- such as:
> 
> global a -- connects to _G.a
> main b -- connects to _ENV.b
> upvalue c -- connects to the "nearest" 'c' identifier of higher scopes
> 
> From what I've learned, the Lua community represented on this list is
> hostile to new identifiers.

  There *is* a method to our madness (or maddening obstinance)---the
introduction of a new keyword *can lead to code breakage.*  I had this issue
when I wrote SPCDNS [1].  One of the structure fields is named "class"
because the RFCs refer to that field as "class".  C++ compilers do not like
it [2][3].

  Perl got around this by using sigils for everything *but* keywords, so in
Perl, the introduction of keywords does not break existing code.

  Algol got around this with very clever parsing where it can deal with
keywords used as identifiers, so this isn't an issue [4].

  Lua doesn't use sigils nor is its parser capable of dealing with keywords
as identifiers.  So, in order to prevent code breakage, some of us Lua users
are conservative when it comes to adding new keywords.

> You could just use _G and _ENV to refer
> to global and environmental scope all the time -- `up c' would ~work~.
> These are just things to think about.  In some ways it would be more
> readable because you would know for certain where you intend to modify
> a "global" (in a liberal sense of the word).  I think Python has these
> keywords because "global scope" is not a data structure you can modify
> like a table would be in Lua.  I am not a Python person -- correct me
> if I'm wrong.  I am entirely undecided on which scoping ideology I
> prefer.

  Ruby uses sigils for scoping, so one sigil for global, one for single
instance per all classes, one per instance of class (class member if I know
my C++ terminology) and local (I think local is where no sigil is used---I
don't know enough Ruby to fully comment on this, but I don't really care for
Ruby all that much personally [5]).

  If a langauge requires the explicit definitions of variables (and this is
orthognal to static-dynamic typing), then the implicit scopeness [6] of a
variable becomes moot since it's easy for the compiler/interpreter to know
what the scope of a variable is, but it comes at the price of some
additional typing [7].

  I'm used to "global by default", as that's what quite a few langauges I
already know do by default (C [8], Forth [9] and Basic [11]).  

  -spc (Who wouldn't mind either "global" or "var" as a way of making
	globals---it would make finding typos just a tad bit easier)

[1]	https://github.com/spc476/SPCDNS

	It's a DNS encoding/decoding library (networking not included), with
	Lua bindings.

[2]	SPCDNS is written in C.  Not C++.  C.  

[3]	That's okay.  I don't like C++.  So it's a mutual hate.

[4]	if if == then then then = else else else = if (or something like
	that)

[5]	Too much "spooky action at a distance" because of the exteme ability
	to monkey patch Ruby to hell and back.

[6]	Is that even a word?

[7]	Which causes a subset of programmers to bitch about all that @@#$@#$
	typing they're forced to endure.  

[8]	Variables declared outside of a function scope.  Without a "static"
	they become global variables (modulo some deep linker magic).

[9]	Forth is somewhat the same issue as Lua---"global" is fluid,
	especially when you have Forth wordlists in play, which work
	similarly to Lua's _ENV.  And even then, a word is "global" only
	until it's re-defined; any existing code using the previous
	definition will continue to use the previous definition, but new
	code will use the new definition [10].

[10]	For those of you who like changing the semantics of a language
	(changing the compiler, adding new language features, etc) you
	really need to look at Forth, where you have access to the internal
	compiler in a standardized way.

[11]	Your typical 8-bit micro BASIC from the 80s.  *EVERYTHING* is
	global there.