lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> A "missing" key *is* a value of nil. there is no difference. (unlike
> in JS where you can assign `undefined` which is treated differently to
> the `delete` operator)

Right, I know what the current behavior is. I was wondering if there was consideration for adding a return value of  `LUA_TNONE` of the new shiny 5.3 APIs in 5.4, to indicate "key not found". That is, upon calling functions like `lua_(raw)get(i/table/global)`, if the key you're looking for doesn't exist, you get back a `LUA_TNONE` return as an `int`, rather than just the nil type indicator. It would still push a `nil` (which would not break any older source code which checks for a nil value as failure and would avoid having to make a new 'undefined'/'none' literal in Lua).

That change might be too heavy-handed, of course, so just having a function `lua_getkey(i/table/global)` and then having this new API exhibit that behavior might be less of a breaking change and allow users of Lua to reason about 'nil' stuck in tables more effectively?

On Sat, Aug 20, 2016 at 2:56 AM, Daurnimator <quae@daurnimator.com> wrote:
On 20 August 2016 at 16:40, ThePhD <jm3689@columbia.edu> wrote:
> As another side note, has there ever been consideration for a better "key
> detection" function? For example, lua_gettable returning the `none` type if
> the requested key doesn't exist, but still pushing `nil`, so that people
> could differentiate between the presence of a nil value mapped to a key
> versus a non-existent key?

A "missing" key *is* a value of nil. there is no difference. (unlike
in JS where you can assign `undefined` which is treated differently to
the `delete` operator)