Re: Readability question.

Francisco Olarte <[email protected]> Fri, 5 Jun 2026 13:37:33 +0200
Newsgroups gmane.comp.lang.lua.general
Message-ID <CA+bJJbzwcYPPwXgRQ4+sGOZwtWL3KLG68AYc-+9s_n-a2os3+Q@mail.gmail.com>
Sainan.

On Fri, 5 Jun 2026 at 13:08, 'Sainan' via lua-l <[email protected]> wrote:
>
> If you have to ask, it's probably not readable. I would make the conditional an actual conditional:

I ask because it is readable for me, but not sure how it is for others.

> if a and b then
>     x = combine(a, b)
> else
>     x = a or b
> end

This is the actual code I have been using, but was tempted to play a
little "lua-golf".

> Now, one might also be able to see more obviously that if a=nil and b=nil then x=nil, so one could write `x = a or b or error()` in the else clause if that's undesired.

Yep, but error avoiding is specifically desired. One specific scenario
in which I use the construct is cascading configuration merging, where
combine being something like a table version of "a+b". For all this
kind of things I normally have an unchecked version ( combine in the
example ) and a checked one ( i.e., replacing x= with return on your
example, I have even been tempted to do

function guard(f) return function(a,b) return (a and b and f(a,b)) or
a or b end end

in which case it would be less problematic ( buried in a lib,
explaining comment, no worries about maintenance, can even have the
equivalent if commented ) ) .

But, after looking at some bytecode, I think the one liner may be
useful on some REPL experiments, but it is not really equivalent if
combine(a,b) returns something false-ish or has multi-returns ( and,
on the returning cases, inhibits tail calls and forces single return
).
Falseish is not normally used by me, but clipping multi-returns is, as
I may want come combine() to return fail, "combine error...".

Thanks for the feedback.

Francisco Olarte.

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CA%2BbJJbzwcYPPwXgRQ4%2BsGOZwtWL3KLG68AYc-%2B9s_n-a2os3%2BQ%40mail.gmail.com.