errors needs to be tostring()'d

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

errors needs to be tostring()'d

Daurnimator
I just noticed that the __tostring metamethod is not invoked when an
error is at the top level in both luajit and lua:

error(setmetatable({},{__tostring=function(o) return "My error msg" end;})

This seems like bad beahviour to me.... makes custom error objects a bit harder.

Reply | Threaded
Open this post in threaded view
|

Re: errors needs to be tostring()'d

Rena
On Sat, Nov 19, 2011 at 02:32, Daurnimator <[hidden email]> wrote:
> I just noticed that the __tostring metamethod is not invoked when an
> error is at the top level in both luajit and lua:
>
> error(setmetatable({},{__tostring=function(o) return "My error msg" end;})
>
> This seems like bad beahviour to me.... makes custom error objects a bit harder.
>
>

Isn't this fixed in 5.2?

--
Sent from my toaster.

Reply | Threaded
Open this post in threaded view
|

Re: errors needs to be tostring()'d

Patrick Donnelly
On Sat, Nov 19, 2011 at 4:37 AM, HyperHacker <[hidden email]> wrote:

> On Sat, Nov 19, 2011 at 02:32, Daurnimator <[hidden email]> wrote:
>> I just noticed that the __tostring metamethod is not invoked when an
>> error is at the top level in both luajit and lua:
>>
>> error(setmetatable({},{__tostring=function(o) return "My error msg" end;})
>>
>> This seems like bad beahviour to me.... makes custom error objects a bit harder.
>>
>>
>
> Isn't this fixed in 5.2?

I would hope not. Why would error call the __tostring metamethod on an object?

--
- Patrick Donnelly

Reply | Threaded
Open this post in threaded view
|

Re: errors needs to be tostring()'d

Luiz Henrique de Figueiredo
> I would hope not. Why would error call the __tostring metamethod on an object?

The standalone interpreter prints the error message and this triggers
the __tostring metamethod.

Reply | Threaded
Open this post in threaded view
|

Re: errors needs to be tostring()'d

Daurnimator
In reply to this post by Patrick Donnelly
On 20 November 2011 01:04, Patrick Donnelly <[hidden email]> wrote:
> I would hope not. Why would error call the __tostring metamethod on an object?
>
> --
> - Patrick Donnelly
>
>

error doesn't; the lua interpreter's main pcall prints off any
un-caught errors: currently if the error isn't a string, you just get:
lua: (error object is not a string)
It would be nice if it did a tostring() on the error message; so that
you could use error objects with a __tostring metamethod: error (
setmetatable({"Failure is ","an option"},{__tostring=table.concat})

See http://www.lua.org/source/5.1/lua.c.html#report

It is the same way in luajit and 5.2

Reply | Threaded
Open this post in threaded view
|

Re: errors needs to be tostring()'d

Roberto Ierusalimschy
> It would be nice if it did a tostring() on the error message; so that
> you could use error objects with a __tostring metamethod: error (
> setmetatable({"Failure is ","an option"},{__tostring=table.concat})
>
> See http://www.lua.org/source/5.1/lua.c.html#report
>
> It is the same way in luajit and 5.2

I don't think it is the same with 5.2:

  roberto@roberto:~/prj/lua$ lua
  Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
  > error (
  >> setmetatable({"Failure is ","an option"},{__tostring=table.concat})
  >> )
  Failure is an option

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: errors needs to be tostring()'d

Daurnimator
On 22 November 2011 00:13, Roberto Ierusalimschy <[hidden email]> wrote:

>> It would be nice if it did a tostring() on the error message; so that
>> you could use error objects with a __tostring metamethod: error (
>> setmetatable({"Failure is ","an option"},{__tostring=table.concat})
>>
>> See http://www.lua.org/source/5.1/lua.c.html#report
>>
>> It is the same way in luajit and 5.2
>
> I don't think it is the same with 5.2:
>
>  roberto@roberto:~/prj/lua$ lua
>  Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
>  > error (
>  >> setmetatable({"Failure is ","an option"},{__tostring=table.concat})
>  >> )
>  Failure is an option
>
> -- Roberto
>
>

Odd; I didn't test with 5.2; but the code for "report" in lua.c is the same :)

In this case; I spose this is something for Mike Pall to fix?