lua-users home
lua-l archive

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


In this case something like t?[a]?[b]?[c]?[d]?[e], would be equivalent to t[a][b][c][d][e], but if any evaluation preceded by a ? has a false value (false or nil), the result of the entire _expression_ is that value (short circuiting). This only short circuits out of chains of index expressions (of all forms), and function calls. Would that be something that would work in lua?

Back in 2013, I added both "safe navigation" and "required field" shorthands to my Lua parser.  So t?[a]?.b will evaluate as nil if any of the component expressions are nil, while t![a]!.b will instead throw an error message like "<_expression_> is missing required field 'b'", if, say, t[a].b==nil.  Safe navigation, in various forms, is pretty easy to add to Lua.  A "required field" semantic is harder to mod in, and it's really just a slightly terser way of writing boilerplate asserts, but I think it's still useful to have around.

Since I wrote that patch, Apple's Swift has become quite popular, and while it's a strongly typed compiled language, Swift's syntax for optional and required fields is fairly similar to what I did inside my Lua parser.  Thanks to Swift, a lot more programmers have become used to '?' and '!' semantics.  That said, the details of how, exactly, the semantics of either ought to work in the context of Lua remain highly debatable (and those details have been debated on this list in the past).

-Sven