3 ms·
iirc, one other confusing element is that tables (hash tables? I don't remember what they're called) return null when a lookup is done for a nonexistent key. T
by _huayra_ 5y ago
iirc, one other confusing element is that tables (hash tables? I don't remember what they're called) return null when a lookup is done for a nonexistent key.
This is not necessarily a bad choice. Exceptions and such can be a real pain. However, accidentally getting a null value because you didn't check and then have it propagate much further in your program is extremely difficult to debug. Instead of blowing up at the site of the bad lookup, you only see the distant effects of it (e.g. put the null value into another table, which gets put in another table, and then is attempted to be called as a function, etc).
- boardwaalk 5y agoIt even does this for globals (which are just another table), so there’s never an “undefined variable” error (even though there really is). It’s pretty unfortunate. You can mitigate it using meta tables though.
- naasking 5y agoStoring a null value is a legitimate operation. How do you distinguish "never stored a value under key K", and "stored null under key K"?
- scgtrp 5y agoI'm pretty sure you're agreeing with them. Current Lua does this, which is wonky: > t = {} > t['a'] = nil > t['a'] nil > t['b'] nil
- otikik 5y agoGive Teal [1] a look. It’s basically typescript for Lua https://github.com/teal-language/tl https://github.com/teal-language/tl