[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: __tostring can return non-strings
- From: Daurnimator <quae@...>
- Date: Fri, 8 May 2015 12:37:10 +1000
In the #lua irc channel today an interesting issue came up:
> t = setmetatable({},{__tostring=function(self) return self end});
> print(type(t), type(tostring(t)), type(tostring(tostring(t))))
table table table
If __tostring returns a non-string non-number, then it is returned to
the caller.
This breaks the assumed invariant that 'tostring' will return a string or nil.
e.g. print will error with: 'tostring' must return a string to 'print'
Please check your programs to make sure you handle this reasonably!