Open MineBill opened 7 months ago
i don't think it's an error, I need to think more about it. Calling the builtin print
will do the correct thing, as that calls tostring(arg)
itself. The correct usage in your case would be print(tostring(vec))
(if this one doesn't work either then it's totally a bug). I need to think what are the implications of directly using lua_tolstring
, as that doesn't print a message for you.
Assuming this is the correct function:
https://github.com/lua/lua/blob/88a50ffa715483e7187c0d7d6caaf708ebacf756/lbaselib.c#L29
You can see that the standard print also calls luaL_tolstring, which will call the __tostring metamethod.
I guess this is a question of which one mani should use. Both options can be valid and correct so ultimately i think this is something that needs to be configurable in some way:
Yeah, you can change it on your side if it fits your usecase better, but i'm still a bit iffy about supporting it officially. Technically speaking, the "correct" definition (not that it will work right now) for a print alternative is print :: proc(arg: any)
, and then the conversion would be done on the print side. Thats why the builtin one works, because it accepts anything, not specifically a string. Having string
as param and requiring an explicit tostring
seems like the correct thing, and the solution to the real problem would be to have better conversion between the generic lua types and odin (probably via any
)
I do see your point, i'll play a bit with accepting that option too and see what happens
Hmm, i guess you are correct. A better solution would be to support any.
When calling an odin proc that expects a string, from lua, lua will not convert usertypes to string because
luaL_checkstring
doesn't attempt to call the__tostring
metamethod, even if it is defined for the usertype. A possible solution is to useluaL_tolstring
which will attempt to call__tostring
for usertypes.Using the above print function to print the vec3 will result in:
bad argument #1 to 'print' (string expected, got vec3)