lua-users home
lua-l archive

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


> I suppose there's no way of having some static variables local to one file ?
> I.e. I would have two files that both have a variable called 'name' at the
> top, which can be used by all the functions in that file,
> but the variable has different values in different files...
> Just like in C...

I think the nearest you can get of this is with *upvalues* (version 3.1).
You can declare a local variable in a file, and functions in these file
can use the value of this local variable:

      local temp = ...
      
      function foo ()
         ... %temp ...              -- use upvalue temp
      end

If another file uses "temp", it is completely independent of this one.

One drawback of this solution is that upvalues are *values*, not *variables*,
so you cannot assign to them. You can go around that by declaring your value
to be a table, and storing the variable inside the table. For instance:

      local temp = {var = ...}

      function foo ()
        %temp.var = ...
        ...  %temp.var ...
      end

In this way, you never have to change the value of "temp" (always the same
table).

-- Roberto