lua-users home
lua-l archive

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


On 22/12/14 15:58, Antonio Scuri wrote:
>  Hi,
> 
>   I would like to write something like this *in a single line*:
> 
> y = (x == nil)? x: x + 1
> 
>   If I do this:
> 
> y = testfunc(x == nil, x, x+1)
> 
>   and implement the "if" inside the function, I have the problem that
> both expressions are evaluated before calling the function, and the
> second expression will produce an error when x is nil.
> 
>   I'm not seeing the solution. Is this possible?
> 
> Thanks,
> Scuri
> 
Lua does not have a ternary operator, however you can preform what you
want with the following code:

y = x and x + 1 or x

See the wiki[1] for further examples

[1] http://lua-users.org/wiki/TernaryOperator

-- 
Liam

Attachment: signature.asc
Description: OpenPGP digital signature