lua-users home
lua-l archive

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


On 11 November 2016 at 02:12, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
> What use do you have in mind for this?
> What problem would it solve?

In a large number of C bindings, people end up writing a __tostring
that simply does what I mentioned. e.g.

  - https://github.com/LuaDist/lrandom/blob/557814218d84db4d2991e86e1eb99fe9c5d65939/lrandom.c#L87
  - https://github.com/daurnimator/lua-psl/blob/54a8a7875795d67d535f2dd8496a29dfe3885878/psl/psl.c#L62
  - https://github.com/luvit/luv/blob/4977c67659cda9f2929b032c9ec7b7491deb63f3/src/req.c#L25

For those that don't, they often end up doing a poor imitation (e.g.
using a delimiter other than ": ", or surrounding in some token).
Which results in unnecessary non-uniformity.

For pure-lua bindings, I've seen people (in the IRC channel usually)
go to great lengths trying to achieve the same in lua.
You may recall various threads here on the mailing list requesting a
'rawtostring'.
e.g. I see stuff like:

```lua
local function raw_tostring(t)
    local old_mt = getmetatable(self)
    setmetatable(t, nil)
    local raw = tostring(self)
    setmetatable(t, old_mt)
    return raw
end

function mt:__tostring()
   return raw_tostring(self):gsub("^userdata: ", "mytype: ")
end
```

It's one of those recurring topics we seem to have. e.g.
http://lua-users.org/lists/lua-l/2004-01/msg00173.html

By grabbing from __name by default, I think we'll see fewer people
writing their own __tostring due to the default being sufficient.
We'll also end up with better debugging of libraries/classes that
never bothered with a __tostring in the first place.