Log In  


PICO-8 print behavior is inconsistent with Lua standard:

mmt={}
mt=setmetatable({},mmt)
t=setmetatable({},mt)

function mmt:__index(k)
 print('mt.'..k)
end

print(t)

This code should not output mt._tostring as, per language manual, I quote: "the access to a metamethod does not invoke other metamethods". You can check it on the official Lua demo too.

1


If I had to guess, I think @zep is calling PICO-8's custom tostr(v) inside print(v, ...), rather than implicitly calling the v:__tostring() metamethod, if present.

PICO-8's tostr(v) probably checks v for custom PICO-8 types and, if it finds a table, it probably checks to see if v.__tostring exists, which will invoke your __index(), before trying to invoke that instead of its own handlers.


Sounds like reasonable assumption. Somewhere in tostr(v) implementation v.__tostring needs to be replaced with rawget(v,'__tostring').


Oups, I mean getmetatable(v).__tostring needs to be replaced with rawget(getmetatable(v),'__tostring'), of course.


Does it, though? Don't we want the API-level function to use the overridden metamethod for the table? If I override __tostring, I want print() and tostr() to honor it.

Edit: Oh wait, I get what you mean. It shouldn't be using the overridden __index to get the value of __tostring. Never mind. Yeah I agree, @zep needs to fix this.



[Please log in to post a comment]