[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Expanding variables at function declarion time
- From: Peter Shook <peter.shook@...>
- Date: Mon, 19 Nov 2001 23:34:18 -0500
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