Re: Ternary operator patch
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Ternary operator patch
- From: Drake Wilson <drake@...>
- Date: 2010年9月12日 16:19:35 -0700
Quoth Jonathan Castello <twisolar@gmail.com>, on 2010年09月12日 15:56:05 -0700:
> Well...
>
> --
> a = ({[true]=function() return 1 end, [false]=function() return 2
> end})[b ~= nil]()
> --
Let me provide an extra data point. In a Lua variant I have sitting
around locally, I use a preprocessor that includes n-ary conditional
operator with equivalent syntax to the if/then/elseif/then/else/end
statement but with expressions instead of statements. It converts the
expression as:
if a then b elseif c then d else e end
==> _PP.expr_if((( a ) and _PP.expr_then( b ))
or (( c ) and _PP.expr_then( d ))
or (_PP.expr_then( e )) or nil)
except without the indentation. expr_then is a support function that
converts nil and false into special light userdata and multiple values
into special tables, and expr_if does the converse. This also permits
the use of
if a then b, c elseif d then e, f else g end
and similar constructions. I haven't tested for performance or
anything along those lines, though I'm reasonably sure the semantics
are sound.
---> Drake Wilson
- References:
- Re: Ternary operator patch, David Kastrup
- Re: Ternary operator patch, Miles Bader
- Re: Ternary operator patch, David Kastrup
- Re: Ternary operator patch, Jonathan Castello
- Re: Ternary operator patch, Roberto Ierusalimschy
- Re: Ternary operator patch, Enrico Tassi
- Re: Ternary operator patch, Doug Rogers
- Re: Ternary operator patch, HyperHacker
- Re: Ternary operator patch, GrayFace
- Re: Ternary operator patch, Jonathan Castello