{"id":3142,"date":"2016-02-10T03:09:32","date_gmt":"2016-02-10T03:09:32","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3142"},"modified":"2016-02-10T03:09:32","modified_gmt":"2016-02-10T03:09:32","slug":"scala-getorelse-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-getorelse-example\/","title":{"rendered":"Scala getOrElse example"},"content":{"rendered":"<p>The &#8220;getOrElse&#8221; function in Scala is a feature designed to help write code that avoids NullPointerExceptions.<\/p>\n<pre lang=\"Scala\">\nscala> val x = null\nx: Null = null\n\nscala> x.toString\njava.lang.NullPointerException\n  ... 33 elided\n<\/pre>\n<p>Null is there to be like Java, but generally &#8220;None&#8221; is used instead:<\/p>\n<pre lang=\"scala\">\nval x = None\nval y = Some(107)\n<\/pre>\n<p>This is similar to Java:<\/p>\n<pre lang=\"java\">\nInteger x = null;\nInteger y = 107;\n<\/pre>\n<p>The difference is that in Scala, the language gives you some nice options.<\/p>\n<pre lang=\"scala\">\nList(None, Some(107)).\n  map( x => x.getOrElse(-1) )\n\nres54: List[Int] = List(-1, 107)\n<\/pre>\n<p>You could also use a function (e.g. to log a warning):<\/p>\n<pre lang=\"scala\">\nList(None, Some(107)).\n  map( x => x.getOrElse( { println(\"error\") } )\n<\/pre>\n<p>You can also do useful things to handle unimplemented APIs &#8211; if you are writing code and you&#8217;re not sure if a branch will ever be used, you can avoid it by marking it as not implemented, with this awesome syntax: <\/p>\n<pre lang=\"scala\">\nList(None, Some(107)).\n  map( x => x.getOrElse(???))\n\nscala.NotImplementedError: an implementation is missing\n  at scala.Predef$.$qmark$qmark$qmark(Predef.scala:225)\n  at $anonfun$1$$anonfun$apply$1.apply(<console>:10)\n  at $anonfun$1$$anonfun$apply$1.apply(<console>:10)\n  at scala.Option.getOrElse(Option.scala:120)\n  at $anonfun$1.apply(<console>:10)\n  at $anonfun$1.apply(<console>:10)\n  at scala.collection.immutable.List.map(List.scala:272)\n  ... 35 elided\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;getOrElse&#8221; function in Scala is a feature designed to help write code that avoids NullPointerExceptions. scala> val x = null x: Null = null scala> x.toString java.lang.NullPointerException &#8230; 33 elided Null is there to be like Java, but generally &#8220;None&#8221; is used instead: val x = None val y = Some(107) This is similar &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-getorelse-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala getOrElse example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480,486],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3142"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3142"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3142\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}