lua-users home
lua-l archive

Quaternary Operator

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


Hi all,
As today seems to be the day to talk about Lua's syntax enhancements,
I would like to talk about another one:
"Quaternary operator"
[ <exp>; true_result; false_result; nil_result ]
meaning
function quaternary (cond,true_result,false_result,nil_result)
 if cond then return true_result end
 if not exists(false_result) then return end
 if cond === false then return false_result end
 if exists(nil_result) then
 return nil_result
 else
 return false_result
 end
end
exists is a compile time function
example
 a = b + [c;100;200] + [d;10;20] + [e;1;2;3]
 b = 1000 c = true d = false
 print(a)
 >>> 1123
Another option is allow the use of comma as separator.
PROs:
- No need of a new lexical symbol like '?'
- Optional handle of nil values
- The square brackets naturally eliminate priority problems.
Cons:
- quite different from C
--
Nilson
-- 
Nilson

AltStyle によって変換されたページ (->オリジナル) /