rawtostring()

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

rawtostring()

Dirk Laurie-2
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.

Reply | Threaded
Open this post in threaded view
|

Re: rawtostring()

Peter Aronoff
Dirk Laurie <[hidden email]> wrote:
> For the umpteenth time I find myself copying this code from one program
> into another:
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

Reply | Threaded
Open this post in threaded view
|

Re: rawtostring()

Sean Conner
In reply to this post by Dirk Laurie-2
It was thus said that the Great Dirk Laurie once stated:
>
> 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


Reply | Threaded
Open this post in threaded view
|

Re: rawtostring()

Dirk Laurie-2
Op Wo., 17 Okt. 2018 om 20:47 het Sean Conner <[hidden email]> geskryf:

>   Why not a rawtostring() function instead?

Look carefully, A rawtostring() function is actually most of my code.

> Why overload tostring()?

Because Adam Moss claims that such a patch was supplied by a member of
the Lua team.

Reply | Threaded
Open this post in threaded view
|

Re: rawtostring()

nobody
In reply to this post by Dirk Laurie-2
On 2018-10-17 19:51, Dirk Laurie wrote:
> For the umpteenth time I find myself copying this code from one
> program into another:
>
> *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.

The sole purpose of that is to get a textual sufficiently unique ID for
the thing?  (I'm guessing...)

You could auto-generate IDs and weaktable them.  Is that good enough?
That avoids tostring() altogether.  (Could also be much more readable
than random hex digits.)

Another fun question:  If a table (or userdata?) is freed and another
one is created later that has exactly the same address, will whatever
you're doing break, or will it still work?

(But yeah, I've also added/used a debug.tostring() before... ^.^)

-- nobody

Reply | Threaded
Open this post in threaded view
|

Re: rawtostring()

Dirk Laurie-2
Op Wo., 17 Okt. 2018 om 21:01 het nobody
<[hidden email]> geskryf:

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

Reply | Threaded
Open this post in threaded view
|

Re: rawtostring()

Dennis Fischer
In reply to this post by Dirk Laurie-2
> For the umpteenth time I find myself copying this code from one program into another

I don't see why. The original tostring result for tables makes almost no sense for anything other than debugging, and if that's all you want, a debug.gettableaddress(tab) function would seem way more concise.