lua-users home
lua-l archive

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


Peter Loveday wrote:
> For example, say I want to create a series of functions which
> simply return their name with an exclamation mark appended to
> it, for each one I might do:
> 
>     -- 'name' contains the name of the function I wish to create
>     setglobal(name, function() return name.."!" end)
> 
> This declaration succeeds, but unfortunately when calling the
> function 'name' refers to the current contents of the name variable,
> not the contents at declaration.  Is there some way to force this
> to be expanded, or the reference frozen, at declaration time ?


upvalues do have their merits.

$ lua
Lua 4.1 (alpha)  Copyright (C) 1994-2001 TeCGraf, PUC-Rio
> function declare(name) setglobal( name, function() return %name..'!' end ) end
> declare'bill'
> declare'bert'
> print(bert(),bill())
bert!   bill!
> 

- Peter