{"id":963,"date":"2013-09-02T10:52:17","date_gmt":"2013-09-02T15:52:17","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=963"},"modified":"2013-09-02T10:52:17","modified_gmt":"2013-09-02T15:52:17","slug":"scala-function-returns-unit-but-should-return-int","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2013\/09\/scala-function-returns-unit-but-should-return-int\/","title":{"rendered":"Scala function returns Unit but should return Int"},"content":{"rendered":"<p>I just started learning Scala last week. I created a stub method in one of my programs but was getting an error. Here&#8217;s my function:<\/p>\n<pre><code>def howMuch(max: Int) {\r\n  var n = 0\r\n  n\r\n}<\/code><\/pre>\n<p>I tried to use the result in expression, e.g.<\/p>\n<pre><code>var m = 1\r\nvar n = howMuch(100)\r\nm += n<\/code><\/pre>\n<p>Here&#8217;s the error I received:<\/p>\n<pre><code>error: overloaded method value - with alternatives:\r\n  (x: Int)Int &lt;and&gt;\r\n  (x: Char)Int &lt;and&gt;\r\n  (x: Short)Int &lt;and&gt;\r\n  (x: Byte)Int\r\n cannot be applied to (Unit)\r\n             m += n<\/code><\/pre>\n<p>It seemed clear to me that my function should be returning an Int, namely <em>zero<\/em>. Why was it returning the Unit value?<\/p>\n<p>Answer: a missing <em>equals sign<\/em> in the function assignment. Here&#8217;s the function that returns an Int:<\/p>\n<pre><code>def howMuch(max: Int) = {\r\n  var n = 0\r\n  n\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I just started learning Scala last week. I created a stub method in one of my programs but was getting an error. Here&#8217;s my function: def howMuch(max: Int) { var n = 0 n } I tried to use the result in expression, e.g. var m = 1 var n = howMuch(100) m += n &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2013\/09\/scala-function-returns-unit-but-should-return-int\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Scala function returns Unit but should return Int<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[313],"tags":[312],"class_list":["post-963","post","type-post","status-publish","format-standard","hentry","category-scala-2","tag-scala"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=963"}],"version-history":[{"count":6,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/963\/revisions"}],"predecessor-version":[{"id":970,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/963\/revisions\/970"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}