The issue with Lua’s and/or in this context is that they don’t work if false or nil are valid values. In a and b or c, if b = false, the result is always c.
I also love null-related operators like ?? and ?. for this, since they explicitly check for null, letting you handle any non-null values for optional/default values. The syntax can get a bit cursed, like maybeNull?.maybeMethod?.(args) in JS, but I still prefer that to writing out multiple field accesses in an if condition… And arguably the code is only less readable if you aren’t acclimated to it.
All that said I do really appreciate Lua’s simplicity, as a language that provides tooling to create the features you want instead of building them into the language, though I wish it had some conventional regex instead of its own patterns.
i despise doing null checks as operators, because everyone does them differently. python’s a is not None is immediately obvious and you don’t have to think about chaining rules.
OK true, technically speaking it is indeed more readable, I guess I really meant that it takes far longer to read. I do admire Lua’s barebones simplicity. Thank you for the “or” tip, I’ve used it a few times before but I often forget about it.
always remember that code is read more than it is written. complex lines need to be deciphered, simple lines don’t. especially fun with symbols that have nonlocal effects like rusts ?.
For real though I actually find them incredibly useful for creating clean and readable code. I wish Lua 5.1 had a ternary syntax.
Ternary, and inline switch (match expressions), as found in functional languages
Oh god yea, replicating switch functionality with a huge column of elifs is so gross.
I’ve survived 11 years of programming without ternary operators and prefer to keep it that way
luas operators are all text in order to be readable. more symbols makes code less readable.
if you want a one line operation that gives a default result, use
or
:a = b or c
is equivalent toif b then a = b else a = c end
.The issue with Lua’s and/or in this context is that they don’t work if false or nil are valid values. In
a and b or c
, ifb = false
, the result is alwaysc
.I also love null-related operators like ?? and ?. for this, since they explicitly check for null, letting you handle any non-null values for optional/default values. The syntax can get a bit cursed, like
maybeNull?.maybeMethod?.(args)
in JS, but I still prefer that to writing out multiple field accesses in an if condition… And arguably the code is only less readable if you aren’t acclimated to it.All that said I do really appreciate Lua’s simplicity, as a language that provides tooling to create the features you want instead of building them into the language, though I wish it had some conventional regex instead of its own patterns.
i despise doing null checks as operators, because everyone does them differently. python’s
a is not None
is immediately obvious and you don’t have to think about chaining rules.OK true, technically speaking it is indeed more readable, I guess I really meant that it takes far longer to read. I do admire Lua’s barebones simplicity. Thank you for the “or” tip, I’ve used it a few times before but I often forget about it.
always remember that code is read more than it is written. complex lines need to be deciphered, simple lines don’t. especially fun with symbols that have nonlocal effects like rusts
?
.