I've seen a lot scripts using "tostring". I was trying to understand what it means but I didn't figured out yet. Help please.
What is "tostring" used for?
1 answer
It makes things into strings
As a cat, I prefer yarn.
tostring simply takes something and returns the string interpretation of it. It's used behind the scenes for print, and isn't necessary in a lot of cases because most things have implicit typecasting in Lua.
Tables and userdata both have their __tostring metamethod invoked if applicable, and it uses the string interpretation of whatever is returned. I'm unsure of whether nil being returned is turned into 'nil' or whether it just gets the memory value for the object at that point, however.
2 comments
Thank you!
tostring is allowed to return non-string values. If you try to print something that does that, though, you get an error.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
tostring(1) turns something into a string.