lua-users home
lua-l archive

Re: Access a member from within the table def

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


On 2010年5月14日 13:15:19 +0200
steve donovan <steve.j.donovan@gmail.com> wrote:
> Always more than one way to do anything...
> 
> local init -- to make sure the function is visible within itself
> function init(val)
> local t = {}
> setfenv(init,t)
> -- we're now writing directly to t, and t's fields are available on the RHS
> x = val
> y = x+10
> z = x+y
> return t
> end
[Maybe I'm wrong on this, but here is my pov:]
Yes, but there's always the same issue that now globals (eg builtin funcs) are inaccessible, since an "env" acts both as globals & locals. Thus, "d=math.square(x*x+y*y)" fails. What is needed (and probably what the op means, but I don't want to speak for him) is to redefine the local scope only, so that the block writes (new vars) and reads (if var exists) in the table itself.
Meaning a table def acts like a func which scope is the table. Pseudo-syntax showing this better than words, maybe:
t = table
 <field defs>
end
(This probably requires under the hood the table to be created empty before the block is executed.)
denis
________________________________
vit esse estrany ☣
spir.wikidot.com

AltStyle によって変換されたページ (->オリジナル) /