lua-users home
lua-l archive

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


On Sat, Jul 28, 2018 at 5:31 PM, Dirk Laurie wrote:
One might think that if the subject is already a string, the
__tostring metamethod is skipped, but that is not the case.


That's really unexpected feature!

BTW, it could be useful for numbers too (to make numbers look more realistic when printed):

> = 1.0, 2^53, math.pi, 1<<62, 0.1
1.0    9.007199254741e+15    3.1415926535898    4611686018427387904    0.1

> debug.setmetatable(0, {__tostring = function(n) return math.type(n)=="integer" and n.."" or ("%.17g"):format(n) end});

> = 1.0, 2^53, math.pi, 1<<62, 0.1
1    9007199254740992    3.1415926535897931    4611686018427387904    0.10000000000000001

Old behavior is still available through concatenation:

> = ""..0.1, ""..1.0
0.1    1.0