{"id":3128,"date":"2016-02-10T00:39:14","date_gmt":"2016-02-10T00:39:14","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3128"},"modified":"2016-02-10T00:39:14","modified_gmt":"2016-02-10T00:39:14","slug":"fixing-scala-error-error-return-outside-method-definition","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/fixing-scala-error-error-return-outside-method-definition\/","title":{"rendered":"Fixing scala error: &#8220;error: return outside method definition&#8221;"},"content":{"rendered":"<p>If you are starting at Scala, and are accustomed to older languages, you may be tempted to sort a list like so:<\/p>\n<pre lang=\"scala\">\nval x = new List(3, 1, 2)\n\nx.sortWith( \n  (a, b) => { \n    return a > b\n  } \n)\n<\/pre>\n<p>This code will throw the following error:<\/p>\n<pre>\nerror: return outside method definition\n<\/pre>\n<p>The intent of this code is to create the equivalent of a Java comparator, using an anonymous function. However, &#8220;return&#8221; is not defined correctly in Scala, and should never be used. <\/p>\n<p>The above example can be rewritten like so:<\/p>\n<pre lang=\"scala\">\nval x = new List(3, 1, 2)\n\nx.sortWith( \n  (a, b) => { \n    a > b\n  } \n)\n<\/pre>\n<p>This implicitly returns a value from the function. The &#8220;return&#8221; keyword returns from a function that is tied to an object (a &#8220;method&#8221;), so if this code was inside a method it would &#8220;work&#8221;, but drop several stack frames.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are starting at Scala, and are accustomed to older languages, you may be tempted to sort a list like so: val x = new List(3, 1, 2) x.sortWith( (a, b) => { return a > b } ) This code will throw the following error: error: return outside method definition The intent of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/fixing-scala-error-error-return-outside-method-definition\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fixing scala error: &#8220;error: return outside method definition&#8221;&#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":[9],"tags":[480,484,485],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3128"}],"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=3128"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3128\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}