lua-users home
lua-l archive

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


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 Adam's posts contains this line:

> 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.