[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Getting the "ID" of an object.
- From: "Dimiter \"malkia\" Stanev" <malkia@...>
- Date: Sun, 01 May 2011 23:51:25 -0700
Not sure I understand exactly, but here is my poor attempt at solving it
(poor because it has local side effects - I have to turn off the
metatable, then turn it back on if tostring is the lua tostring)
local test = {
}
local function print_old_new(obj, old_tostring, args)
return "OLD: " .. old_tostring(obj) .. " NEW: " ..
table.concat(args, ' ')
end
local function wrap_tostring(obj, new_tostring, ...)
local args = { ... }
local mt = getmetatable(obj)
local old_tostring = mt and mt.__tostring or tostring
return function(obj)
local mt = getmetatable(obj)
setmetatable(obj, {})
local r = new_tostring(obj, old_tostring, args)
setmetatable(obj, mt)
return r
end
end
setmetatable(
test,
{ __tostring = function(obj) return "PRIMARY?" end }
)
setmetatable(
test,
{ __tostring = wrap_tostring(test, print_old_new, "SECONDARY") }
)
setmetatable(
test,
{ __tostring = wrap_tostring(test, print_old_new, "ADVANCED!") }
)
print(test)
On 5/1/11 9:56 PM, Lorenzo Donati wrote:
Hi all!
I recently stumbled on an issue I cannot find a workaround for.
When defining the __tostring metamethod for a a table, the old behaviour
is completely lost, i.e. there is no "rawtostring" and no apparent way
to concoct a substitute (without resorting to C).
What I'd like is a way to get the unique IDs (I suppose they are the
memory address) that Lua generates for the default string representation
of tables, i.e. the NNN in
tostring(t) --> "table: NNN"
It doesn't seem that the debug library helps here either.
I need the "NNN" above to include that ID into the string representation
for my OOP objects when defining __tostring.
I know that I could either:
1) generate an unique ID with a custom algorithm
2) upon creation, before attaching the metatable to the object being
created, call tostring on the object and store the ID in a (weak?) table.
3) use a proxy and return the ID of the "private" object instead
(preserves uniqueness because of the 1-to-1 relationship between proxies
and objects)
But both these approaches are lots of boilerplate code and/or useless
overhead (the logic for that ID is already somewhere in the bowel of Lua
- it just need a way to be exposed cleanly).
I wonder if there is a nifty trick to recover that NNN only when needed
without too much overhead or code.
If not, why not adding a new method to the debug library, such as
"debug.getid". Maybe it could be useful in other contexts and it
shouldn't be to "heavy" to implement (just expose what's already in
Lua). I see that upcoming Lua 5.2 has a debug.upvalueid. Maybe rename it
as debug.getid and make it applicable to any Lua object? Wouldn't it be
more in Lua spirit (less functions, with more general application)?
Or even enhance debug.getinfo to give info also on tables (and other
types too?).
Or, still another option, add a second return value to "type" (I admit
that this solution is maybe not very clean semantically).
thanks!