lua-users home
lua-l archive

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


Unless I'm mistaken, upvalues in Lua 5 are NOT read only.
If I am mistaken then someone will probably quickly correct me. :-)
Also, anyone with a compiler handy can check the code easily.

This means that you can accomplish the equivalent of static variables by
doing something like the following....

function MakeFoo()
   local StaticA = 0 -- Declare/Initialize statics here
   local StaticB = 0

   return function(a,b)
         ...
         print(StaticA, StaticB)
         StaticA = StaticA + a
         StaticB = StaticB + b
         ...
      end
end

Foo = MakeFoo()
Foo(1,2)
Foo(1,2)



-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Phil Bewig
Sent: Thursday, March 18, 2004 1:53 PM
To: lua@bazar2.conectiva.com.br
Subject: Static variables


I'm considering lua for a specific project.  One of my needs is for static
variables that retain their value from one invocation of a function to the
next.  In a quick reading of the manual section on variable scoping, I saw
no mention of static variables.  Does lua provide static variables?  Did I
miss something while reading the manual?

Many thanks,

Phil