luau/rfcs/function-debug-id.md
2023-08-13 18:18:07 +00:00

1.1 KiB

debug.id

Summary

Add debug.id which accepts a value and always returns the identifier unique to that value.

Motivation

When debugging, it is often useful to compare identity. Usually print will print the memory address of the table or userdata, but when __tostring is overriden, the address becomes unobtainable.

rawequal can compare equality between two arguments, but is not suitable for a human comparing output between two print calls or between keys or values in a table.

Design

debug.id(value: string | userdata | table | thread | function): string always returns the identifier unique to that value, and is not possible to override. The identifier can be reused after the value associated with it is garbage collected.

Drawbacks

Identifiers being reused has the potential to confuse humans reading them.

Alternatives

Lua has string.format("%p", {}), but this is an invalid format option in Luau and less obvious.

If not implemented, it remains annoying to compare identity when dealing with tables or userdata that override __tostring.