{"id":68,"date":"2016-09-16T17:37:59","date_gmt":"2016-09-16T17:37:59","guid":{"rendered":"http:\/\/67bricks.com\/blog\/?p=68"},"modified":"2016-09-16T17:38:00","modified_gmt":"2016-09-16T17:38:00","slug":"silly-scala-tricks-the-ternary-operator","status":"publish","type":"post","link":"https:\/\/blog.67bricks.com\/?p=68","title":{"rendered":"Silly Scala tricks &#8211; the Ternary operator"},"content":{"rendered":"<p>Scala doesn&#8217;t use the ternary operator:<\/p>\n<blockquote><p>\u00a0 test ? iftrue : iffalse<\/p><\/blockquote>\n<p>preferring to use if:<\/p>\n<blockquote><p>\u00a0 if (test) iftrue else iffalse<\/p><\/blockquote>\n<p>Dan and I (Inigo) were on a long train journey together and decided to see if we could use Scala&#8217;s flexible syntax and support for domain-specific languages to implement our own ternary.<\/p>\n<p>Our best shot is:<\/p>\n<blockquote>\n<pre>class TernaryResults[T](l : =&gt; T, r : =&gt; T) {\r\n  def left = l\r\n  def right = r\r\n}\r\n\r\nimplicit class Ternary[T](q: Boolean) {\r\n  def ?=(results: TernaryResults[T]): T = if (q) results.left else results.right\r\n}\r\n\r\nimplicit class TernaryColon[T](left : =&gt; T) {\r\n  def \u2af6(right : =&gt; T) = new TernaryResults(left, right)\r\n}\r\n\r\n(1+1 == 2) ?= \"Maths works!\" \u2af6 \"Maths is broken\"\r\n\r\n<\/pre>\n<\/blockquote>\n<p>This is pretty close &#8211; it&#8217;s abusing Unicode support to have a &#8220;colon-like&#8221; operator since colon is already taken, and it needs to use ?= rather than ? so the operator precedence works correctly, but it&#8217;s recognizably a ternary operator, and it evaluates lazily.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scala doesn&#8217;t use the ternary operator: \u00a0 test ? iftrue : iffalse preferring to use if: \u00a0 if (test) iftrue else iffalse Dan and I (Inigo) were on a long train journey together and decided to see if we could use Scala&#8217;s flexible syntax and support for domain-specific languages to implement our own ternary. Our &hellip; <a href=\"https:\/\/blog.67bricks.com\/?p=68\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Silly Scala tricks &#8211; the Ternary operator&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":1,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}