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.

P#134816 2023-09-24 00:49 ( Edited 2023-09-24 16:32)

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.

P#134952 2023-09-27 10:13

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

P#134976 2023-09-27 15:35

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

P#135025 2023-09-28 08:32

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.

P#135134 2023-09-29 23:41 ( Edited 2023-09-29 23:44)

[Please log in to post a comment]