.:HyPeX:. Posted November 30, 2015 Share Posted November 30, 2015 I've been figguring how this syntax should be written but i keep getting a failure on whenever the result is true. outputChatBox((table.size(attr) and table.size(attr)>0 ) or "no attributes") My simple idea is that the "And" should work as an if to shorten the lines of code, but it only works properly when the table is 0.. How would it be properly written, if possible? EDIT: finally found the reference and it was just as easy as swiping them arround, for those interested: outputChatBox((table.size(attr)>0 and table.size(attr) ) or "no attributes") http://www.lua.org/pil/3.3.html Link to comment
myonlake Posted December 1, 2015 Share Posted December 1, 2015 It's a ternary operator. Standard solution: and/or A frequently used and highly recommend solution is to combine the and and or binary operators in a way that closely approximates the ternary operator: x = a and b or c x = a and b or c and d or e See the book ProgrammingInLua or ExpressionsTutorial for details on the special properties of these binary operators that allow them to work this way. print('x is ' .. (x < 0 and 'negative' or 'non-negative')) -- this works! The main caveat is that if a or c evaluates to true while b or d respectively evaluate to false, then this expression will not behave exactly like the ternary operator. Here, "evaluate to false" means that the value is either false or nil, and "evaluate to true" means not evaluate to false. In the first line above, a and b or c is interpreted as (a and b) or c (because and has higher precedence than or), and if a evaluates to true, then the expression becomes b or c, and if b evaluates to false, then the expression becomes c (not b as you might want). Often, as in the case of our original example, the second operand of the tertiary operator can never evaluate to false, so you are free to use this idiom, but beware of the caveat. If the b will evaluate to false, change the a so that it evaluates exactly opposite and therefore swaps b and c print((x < 0 and false or true)) -- this fails! print((x >= 0 and true or false)) -- this works! Source: http://lua-users.org/wiki/TernaryOperator Link to comment
.:HyPeX:. Posted December 1, 2015 Author Share Posted December 1, 2015 It's a ternary operator.Standard solution: and/or A frequently used and highly recommend solution is to combine the and and or binary operators in a way that closely approximates the ternary operator: x = a and b or c x = a and b or c and d or e See the book ProgrammingInLua or ExpressionsTutorial for details on the special properties of these binary operators that allow them to work this way. print('x is ' .. (x < 0 and 'negative' or 'non-negative')) -- this works! The main caveat is that if a or c evaluates to true while b or d respectively evaluate to false, then this expression will not behave exactly like the ternary operator. Here, "evaluate to false" means that the value is either false or nil, and "evaluate to true" means not evaluate to false. In the first line above, a and b or c is interpreted as (a and b) or c (because and has higher precedence than or), and if a evaluates to true, then the expression becomes b or c, and if b evaluates to false, then the expression becomes c (not b as you might want). Often, as in the case of our original example, the second operand of the tertiary operator can never evaluate to false, so you are free to use this idiom, but beware of the caveat. If the b will evaluate to false, change the a so that it evaluates exactly opposite and therefore swaps b and c print((x < 0 and false or true)) -- this fails! print((x >= 0 and true or false)) -- this works! Source: http://lua-users.org/wiki/TernaryOperator Thanks, that indeed looks interesting, kinda got lost a few times reading the chunk, but after a re-read or two i started getting the idea Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now