lua-users home
lua-l archive

Re: Access to variable names at assignment

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


Scott escribió:
> I am trying not to do something like:
> object_1 = MyObject("object_1")
> or something similar like
> MyObject(object_1);
> especially if there is a cleaner way to do it using the API. I have
> tried to use tagmethods without any success. Is this something that
> can't be done? Again forgive me if this is a foolish question, I'm
> still pretty new to Lua development; and I am still getting my feet
> wet..
You need to use the setglobal tagmethod (Lua 4) or the __newindex
metamethod on the global table (Lua 5). The key provided as the argument is
the name of the global.
Alternatively, you could do this:
function MyObject(name)
 -- construct the object however you want to.
 getglobals()[name] = newobject
 return newobject
end
and then call it like this:
MyObject "object_1"
which would have the side_effect of setting the global object_1.
I think the first solution is cleaner but I use the second one in some
configuration files because it looks better.
R.

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