lua-users home
lua-l archive

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


>In C, for instance, if you write
>
>  void f (void) {
>    i = i+1;
>  }
>
>this `i' is not "automatically local". If you want a local `i', you must 
say so. 

Sorry, if I'm wrong. But as I think you have to declare  a variable (here 
i). If you do

int i;
void f (void) {
  i = i+1;
}

then i is global, but if you declare i as int IN the function f, like:

void f (void) {
  int i;
  i = i+1;
}

then i is local. The same for at least pascal (even I did not write it for 
years ;-). I don't know the other languages.

And this is why I was surprised, that I have to "declare" a variable as 
local in the body of a function. 

Vincent wrote:
>It's mainly because of the original goal of Lua: to be used as a
>configuration language, where data to be set must then be read by
>some other code.
Normally I do use functions to use the code in it several times in a 
program. If I do not encapsulate the internals (by using local variables) 
then probably I would change globals, where it would be better not to do 
so. This has nothing to do if a language is small/embedded or not, as I 
think. If one decides to have local variables at all, then it would make 
sense to restrict it to the block of code it appears in (maybe the global 
one)

As far as I understood in Lua a function definition is just like adding 
content to a table. So what about making a variable just be valid for 
table it appears in? IN lua 4.0beta all globals are part of a table too. 
So it should be easy to implement and easy to understand. 

--------------------------------------------------------------------------------------------------------
Martin Döring, Systemtechnik (IDT)
MAN Nutzfahrzeuge AG
Dachauerstraße 667
D-80995 München

Tel.:   +49(0)89 / 1580 - 1199
Fax:   +49(0)89 / 1580 - 91-1199
E-Mail: Martin_Doering@mn.man.de