Talk:IfElse: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
No edit summary |
||
Line 12: | Line 12: | ||
<syntaxhighlight lang="lua">moo = ({ [true] = ifTrue, [false] = ifFalse })[condition]</syntaxhighlight> | <syntaxhighlight lang="lua">moo = ({ [true] = ifTrue, [false] = ifFalse })[condition]</syntaxhighlight> | ||
But your code is much better. Thanks for posting this. ;D [[User:NeonBlack|NeonBlack]] 22:00, 18 May 2009 (CEST) | But your code is much better. Thanks for posting this. ;D [[User:NeonBlack|NeonBlack]] 22:00, 18 May 2009 (CEST) | ||
---- | |||
The problem with the plain Lua version is that 'ifTrue' is also part of the test condition. | |||
Consider this: | |||
<syntaxhighlight lang="lua"> | |||
variable = true | |||
ifTrue = false | |||
ifFalse = true | |||
moo = (varible) ? ifTrue : ifFalse | |||
== false | |||
</syntaxhighlight> | |||
ElseIf works fine here: | |||
<syntaxhighlight lang="lua"> | |||
moo = IfElse( varible, ifTrue , ifFalse ) | |||
== false | |||
</syntaxhighlight> | |||
Lua's and's 'n' or's however may give unexpected results: | |||
<syntaxhighlight lang="lua"> | |||
moo = (variable) and (ifTrue) or (ifFalse) | |||
moo == true | |||
</syntaxhighlight> | |||
[[User:Ccw|MrSmartArse]] 08:46, 4 June 2009 (CEST) | |||
---- |
Revision as of 07:52, 4 June 2009
There's not really any use for this, as far as i can tell. Lua supports this by its syntax sugars:
moo = (variable) and (ifTrue) or (ifFalse)
works the same as
moo = (varible) ? ifTrue : ifFalse
in C++.
oO You're right. Never thought of that. What I was thinking about, too, was something like
moo = ({ [true] = ifTrue, [false] = ifFalse })[condition]
But your code is much better. Thanks for posting this. ;D NeonBlack 22:00, 18 May 2009 (CEST)
The problem with the plain Lua version is that 'ifTrue' is also part of the test condition. Consider this:
variable = true ifTrue = false ifFalse = true moo = (varible) ? ifTrue : ifFalse == false
ElseIf works fine here:
moo = IfElse( varible, ifTrue , ifFalse ) == false
Lua's and's 'n' or's however may give unexpected results:
moo = (variable) and (ifTrue) or (ifFalse) moo == true
MrSmartArse 08:46, 4 June 2009 (CEST)