Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is "tostring" used for?

Asked by
deris88 146
8 years ago

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.

0
tostring(1) turns something into a string. theCJarmy7 1293 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

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.

0
Thank you! deris88 146 — 8y
0
tostring is allowed to return non-string values. If you try to print something that does that, though, you get an error. BlueTaslem 18071 — 8y
Ad

Answer this question