lua-users home
lua-l archive

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


--- Luiz Carlos Silveira <luiz@FabricaDigital.com.br>
> What would happen if we have a variable "found" that
> is global
> and we call the routine below:
> 
> function find(element, table)
>  i:=1
>  while ((not found) and (i<getn(table)) do
>   if (table[i] == element) then
>    found:=i
>   end
>   i:=i+1
>  end
>  return found
> end
> 
> Do you mean that "found" would only be treated as
> local if an element was found ?

No matter what the syntactic mechanism is, I think
local/global declarations should be resolved 
lexically, not at runtime.

> Or you are proposing that this function gets
> translated into the current lua
> semantics (in a kind o preprocessing) to something
> like:
> 
> function find(element, table)
> local found ...
> 
> (this way, "found" will always be initialized with
> the value 'nil') ?

Yes.  I suspect that that gives rise to behavior
that is least surprising to Lua programmers.  I
think the above code is fairly common and
people will write it, and other scoping rules
would likely lead to more errors.

More concretely, any variable that 
occurs anywhere on the left hand side of
":=" anywhere in a function would be declared 
local to the function (not just local to the block).

Tom.



__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/