lua-users home
lua-l archive

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


> > 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!

I'm really confused: what's wrong with

> function decl(name)\
setglobal(name, function () return name .. "!" end)\
end
> decl"foo"
> decl"bar"
> print(foo(), bar())
foo!    bar!

-- 
http://sc3d.org/rrt/ | perfect, a.  unsatirizable