Discussion:
rawtostring()
Dirk Laurie
2018-10-17 17:51:26 UTC
Permalink
For the umpteenth time I find myself copying this code from one
program into another:

local original_tostring = tostring

local rawtostring = function (val)
local mt = getmetatable(val)
local __tostring = mt and mt.__tostring
if __tostring then mt.__tostring = nil end
local str = original_tostring(val)
if __tostring then mt.__tostring = __tostring end
return str
end

tostring = function (val,raw)
if raw then return rawtostring(val)
else return original_tostring(val)
end
end

For the umpteenth time I wonder whether I am the only Lua programmer
that has ever wished that 'tostring' had an optional second parameter
allowing one to bypass the __tostring metametod.

So I consult the list archive and see that Adam D. Moss asked for this
iin January 2004
http://lua-users.org/lists/lua-l/2004-01/msg00173.html
and twice more that year posted plaintive follow-ups. Similar requests
appear many times after that. I am shocked to see I posted two of them
myself.
One of the esteemed Lua authors (sorry, I forget who!) posted a fairly trivial
patch for lua5.0 which adds a parameter to tostring() for this purpose.
I can't find that patch either in the list archives or on the Wiki.
Peter Aronoff
2018-10-17 18:37:53 UTC
Permalink
For the umpteenth time I find myself copying this code from one program
snip
For the umpteenth time I wonder whether I am the only Lua programmer
that has ever wished that 'tostring' had an optional second parameter
allowing one to bypass the __tostring metametod.
I have no real suggestions about getting this method directly into Lua, but
I will say this: please consider bundling it as a rock and adding it to
LuaRocks.

I know it might seem trivial to you now, but rather than cut and paste,
better to have the rock and make it available to everyone else. (I say this
as someone who was persuaded by Pierre and Hisham to share my split code as
a rock[1], and it’s now reasonably popular.)

Best, Peter

[1] https://luarocks.org/modules/telemachus/split
--
We have not been faced with the need to satisfy someone else's
requirements, and for this freedom we are grateful.
Dennis Ritchie and Ken Thompson, The UNIX Time-Sharing System
Sean Conner
2018-10-17 18:47:31 UTC
Permalink
Post by Dirk Laurie
For the umpteenth time I wonder whether I am the only Lua programmer
that has ever wished that 'tostring' had an optional second parameter
allowing one to bypass the __tostring metametod.
For the record, I have not once wished that.

Why not a rawtostring() function instead? Why overload tostring()?

-spc
Dirk Laurie
2018-10-17 18:58:39 UTC
Permalink
Post by Sean Conner
Why not a rawtostring() function instead?
Look carefully, A rawtostring() function is actually most of my code.
Post by Sean Conner
Why overload tostring()?
Because Adam Moss claims that such a patch was supplied by a member of
the Lua team.
Dirk Laurie
2018-10-17 20:31:12 UTC
Permalink
Op Wo., 17 Okt. 2018 om 21:01 het nobody
The sole purpose of that is to get a textual sufficiently unique ID for
the thing? (I'm guessing...)
Actually, this time I need it for debugging some very complicated
__tostring routines :-) so that I can print tables without their buggy
metamethods being called.

Loading...