lua-users home
lua-l archive

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

On Sat, Aug 06, 2005 at 01:34:03AM -0500, Rici Lake wrote:
> >"declaring" is entirely different in Lua and C (the word hardly applies
> >to globals in Lua).  That's why the comparison seems mostly 
> >meaningless.
> I just can't resist a challenge.
> rlake@freeb:~/src/cdecl$ cat a.c
> a = 0;

Gross, of course, but this is also an explicit variable declaration;
only the "int" keyword is implicit (which, as you mentioned, isn't
allowed anymore).

> OK, it's different than Lua. But completely different? I think not.

In C, you always declare a variable explicitly.  In Lua, you can
"declare" a local or global explicitly, but the comparison here is
to the default scope of implicit declarations.  The parallel being
drawn was between implicit variable creation in Lua and mere *use*
in C, which is (pardon the cliche) apples and oranges.

> Leaving aside Lua's ability to define functions inside functions, which 
> standard C lacks altogether, a local declaration inside a Lua function 
> differs from a variable declaration inside a C function only in that 
> the C variable is statically-typed. The scoping is identical, aside 
> from the fact that Lua, unlike C, implicitly declares the variables in 
> a for statement.

A local declaration inside a Lua function is an explicit declaration:

  function foo() local a = 1 end

We're talking about implicit declarations, using a default scope:

  function foo() a = 1 end

There's no "default scope" of declared variables in C.  Saying "a = 1"
inside a Lua function can implicitly create the variable, but in C you
always have an explicit declaration somewhere.  In practice, the
complaint is that it makes it easy to accidentally create globals in
Lua without the language letting you know.  (I do find that annoying;
I always use "use strict" in Perl.)

Glenn Maynard