lua-users home
lua-l archive

Re: Ternary operator patch

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


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

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