lua-users home
lua-l archive

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

Firstly, environment tables are per-function, whereas all functions
share a single metatable, so function environments could not be set
via metatable. Secondly, separating the environment from the metatable
allows for one to be per-instance and one to be per-class. In the case
of full userdata, if environment tables were set via metatable, then
the common paradigm of setting the metatable to the userdata's class
and the environment table for per-instance data would no longer work,
as the environment table could not be set per-instance without the
metatable being per-instance.

On Mon, Jul 6, 2009 at 9:20 PM, Rob Kendrick<> wrote:
> Hi,
> I just had a thought; why are function environments not implemented via
> a field in a metatable?  For example;
>        setmetatable(func, { _env = sometable })
> rather than;
>        setfenv(func, sometable)
> While I can't see how it makes any functional difference to how they
> are done, it does mean there is only one interface the programmer has
> to remember rather than one, as well as removing two functions that
> pollute the global namespace.  It also feels more orthogonal and
> consistent.
> What am I missing?
> B.