In lua, if we try to index a series of nested table (any of which can be nil), we might write code like this:
-- try to get a.b.c
local ret
if a then
local b = a.b
if b then
ret = b.c
end
end
Which I think is lenthy and ugly. There are also simpler ways:
-- method 1
local ret = a and a.b and a.b.c
-- method 2
local ret = ((a or {}).b or {}).c
I know I'm splitting hairs, but these two methods are still not perfect, method 1 causes more index operations (do a.b twice), method 2 create some unnecessary tables.
So I wonder if there is a way with no side-effect like '?.' in C#? Or '?.' operator violates lua's design philosophy?
++
or+=
, yet you ask for null conditional operator? This is too ahead of its time.a and a.b
equivalent toa?.b
in C#?