[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Access a member from within the table def
- From: steve donovan <steve.j.donovan@...>
- Date: Fri, 14 May 2010 18:42:14 +0200
2010/5/14 spir ☣ <denis.spir@gmail.com>:
> 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.
This is merely a technical problem ;) After creating the table, attach
a metatable with __index = _G (or whatever the enclosing scope is) -
i.e. just like package.seeall. But unlike that case, we can set the
metatable to nil before returning it, because we only need the
'constructor' to operate inside this table scope.
Apart from the clumsiness of the syntax (explicit closure) the issue
is that this is not a table constructor and in fact Lua will be upset
with extra commas!
At the risk of speaking heresy, one can always use macros to make the
whole thing more palatable, although I'm personally uneasy with that.
steve d.