[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Redirecting console output
- From: RLake@...
- Date: Mon, 24 Feb 2003 21:46:11 -0500
function print(...)
for i = 1, arg.n do arg[i] = tostring(arg[i])
console.out(table.concat(arg, "\t"))
end
----
In Lua 5.0 beta, this works because tostring converts anything to a string
and print just calls tostring (from the global environment).
Note undocumented feature in Lua 5.0 beta: if present, the metamethod
__tostring will be called by tostring (and therefore by print). This must
return a string or a number if you want print to work properly.
However, it is often just as useful to redefine the global tostring. I find
this quite useful for debugging:
do
local names = setmetatable({}, {__mode = "k"})
local _tostring = tostring
function tostring(x)
return (x and names[x]) or _tostring(x)
end
local nameable = {["function"] = true, table = true, userdata = true,
thread = true}
function Name(x, name)
if nameable[type(x)] then names[x] = name end
end
end
The following is also quite useful for debugging, given the above:
do
local named = {}
setmetamethod(getglobals(), {
__index = named,
__newindex = function(t, k, v)
Name(k, v)
named[k] = v
end
}
end
I apologise in advance for any typos which might have crept in there... I'm
not doing this email on my Lua machine :)