Hacker News new | past | comments | ask | show | jobs | submit login

it silently returns nil when accessing non-existent elements

Is that similar to what JS does with undefined, or am I missing something?




It is mostly the same, though in my experience it is easier to stop this in Lua than in JS (just attach a metamethod to the globals table to raise an error on lookups for keys with undefined values... this is similar in practice to JS strict mode).

I'd add that most worries about silent nil returns stem from not accepting Lua's very clear value semantics; nil literally means "undefined value" (as compared to JS's null or Python's None); setting a variable to nil is the idiomatic way to release its value for garbage collection.

So "silently returning nil" is more like "explicitly returning notice that the value is undefined". Whether you want that to be an error-raising-event or not is up to you, the programmer. And it's easy to control that error-throwing behavior in a granular way; you can attach the error-throwing metamethods to the globals table, or to specific tables you care to protect.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: