[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Globals vs. Locals
- From: David Given <dg@...>
- Date: Sat, 6 Aug 2005 00:00:07 +0100
On Friday 05 August 2005 21:21, Vijay Aswadhati wrote:
[...]
> C# provides an overloaded feature using the 'using' [1] keyword that is
> pretty nice - sugar-wise; with some adaptation a feature like that provides
> a nice balance and can solve to a certain degree the global versus local
> debate.
C++ has 'using', too, and like a lot of things C++ does, gets it wrong... (you
can't import a symbol from one namespace into another *and give it a
different name*. You can create an alias for a type in a different namespace
with typedef, but if your symbol isn't a type you're doomed.)
[...]
> using ( table, string, ... )
> do
The issue with this is that Lua doesn't really have locals vs. globals. It has
locals defined in this scope vs. locals defined in an outer scope vs. locals
defined in the scope outside that, etc.
(Strictly, Lua's globals aren't variables --- all that happens is that if the
parser sees a symbol it doesn't recognise, it converts it into a lookup of
the globals table. They're quite different things to real variables, defined
with the 'local' keyword.)
So with your suggestion, I can see two main problems: the minor one is that I
can't say *where* I want to import the symbols, but the major one is that
it's loads of typing. Say we've got this:
local init = function (object, param1, param2, param3, param4, param5, param6)
local createobject = function ()
return param1 + param2 + param3 + param4 + param5 + param6
end
object.t1 = createobject()
object.t2 = createobject()
...etc...
end
With explicit importing from an outer scope into an inner scope, I have to
declare my parameter list twice. It's loads more typing and makes me rather
less inclined to use this feature.
(C++ is even worse. Trying to declare a nested function in C++ involves having
to create an inner class with a static method containing your function... and
C++ doesn't have nested scopes, so you have to explicitly pass all your
parameters into the nested function! The pain! The typing!)
(Forth is way better. Creating a Forth function is four characters, plus the
name. They say that a good Forth function can be expressed clearly in one
line of code. This encourages fine-grained factoring and code reuse, and goes
a long way to explain how Forth code tends to be about half the size of the
equivalent C.)
(Actually, I'd love a minimum-typing lambda expression syntax in Lua. I'd very
much like to be able to say something like [ return 4 ] and have it turn into
a nested function; there's a lot of cool stuff you can do with syntax like
this, such as defining your own control-flow routines. See Smalltalk, which
has *no* control-flow syntax in the language. It uses closures for
everything.)
[...]
> -- somewhere else, foo could be invoked like this:
> foo (a1, a2, a3) using (table, string)
[...]
> I wonder if something like this could be achieved using LHF's proposed
> preprocessor and the setenv/getenv functions.
...or I could be misunderstanding you entirely. Are you talking about lexical
scoping or run-time scoping?
--
"Curses! Foiled by the chilled dairy treats of righteousness!" --- Earthworm
Jim (evil)
Attachment:
pgpVLMczV90c5.pgp
Description: PGP signature