{"id":53,"date":"2012-05-10T15:15:46","date_gmt":"2012-05-10T20:15:46","guid":{"rendered":"http:\/\/www.playwithlua.com\/?p=53"},"modified":"2012-05-11T11:58:23","modified_gmt":"2012-05-11T16:58:23","slug":"ternary","status":"publish","type":"post","link":"http:\/\/www.playwithlua.com\/?p=53","title":{"rendered":"Ternary"},"content":{"rendered":"<p>Another short post. It&#8217;s because I&#8217;m working on something.<\/p>\n<p>A lot of languages have a ternary operator, that lets you do this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #993333;\">int<\/span> x <span style=\"color: #339933;\">=<\/span> foo <span style=\"color: #339933;\">?<\/span> <span style=\"color: #0000dd;\">1<\/span> <span style=\"color: #339933;\">:<\/span> <span style=\"color: #0000dd;\">2<\/span><span style=\"color: #339933;\">;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>It&#8217;s an easy way to pack a conditional on to one line, especially useful when giving things default values. Another way is if the <code>if<\/code> statement returns a value, like in Ruby:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"ruby\" style=\"font-family:monospace;\">x = <span style=\"color:#9966CC; font-weight:bold;\">if<\/span> foo\n        <span style=\"color:#006666;\">1<\/span>\n    <span style=\"color:#9966CC; font-weight:bold;\">else<\/span>\n        <span style=\"color:#006666;\">2<\/span>\n    <span style=\"color:#9966CC; font-weight:bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p><!--more--><\/p>\n<p>Lua doesn&#8217;t have this. The best we can do is just write the if statement, which is unwieldy, especially if it&#8217;s several lines long:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">local<\/span> x\n<span style=\"color: #aa9900; font-weight: bold;\">if<\/span> foo <span style=\"color: #aa9900; font-weight: bold;\">then<\/span> x <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #cc66cc;\">1<\/span> <span style=\"color: #aa9900; font-weight: bold;\">else<\/span> x <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #cc66cc;\">2<\/span> <span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Well, I noticed that we have another way of doing this. It turns out I&#8217;m not the first person to think of this, it was pointed out in the <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/lua-users.org\/wiki\/TernaryOperator');\"  href=\"http:\/\/lua-users.org\/wiki\/TernaryOperator\">wiki<\/a>, which is actually probably where I got it from. But it&#8217;s useful:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">local<\/span> x <span style=\"color: #66cc66;\">=<\/span> foo <span style=\"color: #aa9900; font-weight: bold;\">and<\/span> <span style=\"color: #cc66cc;\">1<\/span> <span style=\"color: #aa9900; font-weight: bold;\">or<\/span> <span style=\"color: #cc66cc;\">2<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Because boolean operators return values (other than true\/false) and they have precedence like you&#8217;d expect, this works perfectly. You can even chain them together:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">local<\/span> x <span style=\"color: #66cc66;\">=<\/span> foo <span style=\"color: #aa9900; font-weight: bold;\">and<\/span> <span style=\"color: #cc66cc;\">1<\/span> <span style=\"color: #aa9900; font-weight: bold;\">or<\/span> bar <span style=\"color: #aa9900; font-weight: bold;\">and<\/span> <span style=\"color: #cc66cc;\">2<\/span> <span style=\"color: #aa9900; font-weight: bold;\">or<\/span> <span style=\"color: #cc66cc;\">3<\/span><\/pre><\/div><\/div><\/div>\n\n<p><strong>Update:<\/strong> <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/twitter.com\/#!\/pchapuis');\"  href=\"http:\/\/twitter.com\/#!\/pchapuis\">Pierre Chapuis<\/a> on Twitter points out that this doesn&#8217;t work when the first possible value is false:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">local<\/span> x <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #aa9900;\">true<\/span> <span style=\"color: #aa9900; font-weight: bold;\">and<\/span> <span style=\"color: #aa9900;\">false<\/span> <span style=\"color: #aa9900; font-weight: bold;\">or<\/span> <span style=\"color: #cc66cc;\">3<\/span><\/pre><\/div><\/div><\/div>\n\n<p>He&#8217;s right and you should be careful about that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another short post. It&#8217;s because I&#8217;m working on something. A lot of languages have a ternary operator, that lets you do this: int x = foo ? 1 : 2; It&#8217;s an easy way to pack a conditional on to one line, especially useful when giving things default values. Another way is if the if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/53"}],"collection":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=53"}],"version-history":[{"count":0,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}