lua-users home
lua-l archive

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


> function ifq(condition, f_true, f_false, f_nil)
>  if condition == nil then
>    return f_nil or and f_nil()
>  elseif condition then
>    return f_true and f_true()
>  else
>    return f_false and f_false()
>  end
> end
>

It is very nice, but I think it is not the same.

local f
print( [ f ;1; 2], [f; 1,2,3], [f;1;2;nil] )
>> 2  3 nil

print( ifq(f,1,2) , ifg(f,1,2,3), ifg(f,1,2,nil)  )
>> nil  3 nil

But it is possible to write a function to produce the same result as
is entirely possible to make  multiplication using only additions.

This operator is just a exercise to define a more powerful C-like
ternary operator without new lexical symbols or colon usage - just
syntax. And as in Lua "not nil == not false", I think that could exist
an operator able to check if a value is true, false or nil in one
single step, hence a "tristate/tribool ternary operator" (avoiding
"quaternary operator") makes sense.  Besides that, the bracket syntax
also facilitates understanding in complex expressions. Compare:

  x = 2 + a ? b : c ? d * 2 : e + 5     // C#, C++, C
with
  x = 2 + [ a; b; [c; d * 2; e + 5] ]

And finally, as you don't need to press the shift key, the typing is
slightly faster.

But no reason to stress, for now this is just a exercise.

-- 
Nilson