types.Instance.tostring
Method on every
instancetostring(): any
Converts the instance to a string.
Return value
The result of the instance's class's _tostring metamethod, if it defines one. Otherwise the VM's generic default, "(instance : 0x" + address + ")".
Notes
Unlike a class, an instance does consult a metamethod here: this is the only built-in type-method where a class-level definition (_tostring) reaches the instance's own conversion, since instance indexing goes through the class's delegate chain while a class's own indexing does not. See types.Class.tostring for the class object's own, unconditional default.
Since the fallback address varies from run to run, examples on this site can only check a fixed prefix of the result, not the full string, unless the class defines _tostring.
Example
class Talkative {
function _tostring() { return "I am talkative" }
}
println("Talkative().tostring() =", Talkative().tostring())
class Silent { x = 1 }
println("Silent().tostring().slice(0, 11) =", Silent().tostring().slice(0, 11))Output:
Talkative().tostring() = I am talkative
Silent().tostring().slice(0, 11) = (instance :See also
tostring | Converts the class to a string. |
getmetamethod | Looks up one of this instance's class's metamethods by name, without triggering it. |
| instance | class index |