{"id":2669,"date":"2015-09-23T01:42:57","date_gmt":"2015-09-23T01:42:57","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2669"},"modified":"2015-09-23T01:42:57","modified_gmt":"2015-09-23T01:42:57","slug":"fixing-scala-error-error-recursive-value-count-needs-type","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/fixing-scala-error-error-recursive-value-count-needs-type\/","title":{"rendered":"Fixing scala error &#8220;error: recursive value count needs type&#8221;"},"content":{"rendered":"<p>If you try to define a recursive function without a type, you will get this error:<\/p>\n<pre>\nerror: recursive value count needs type\n<\/pre>\n<p>For example, note how this &#8220;count&#8221; value doesn&#8217;t have a type:<\/p>\n<pre lang=\"scala\">\nval count = \n  (values: Seq[Any]) => { \n    values.headOption match { \n      case Some(_) => 1 + count(values.tail) \n      case None => 0 \n    }\n  }\n<\/pre>\n<p>The fix is fairly simple (although the type is hard to read):<\/p>\n<pre lang=\"scala\">\nval count: (Seq[Any]) => Int = \n  ...\n<\/pre>\n<p>I found in doing this that I had to switch from &#8220;Seq&#8221; to &#8220;Seq[Any]&#8221;. The &#8220;=>&#8221; differentiates the arguments (on the left, in parens) from the return type (on the right). The reason for this is that the type inference algorithm isn&#8217;t set up to handle this &#8211; while this is a simple example, recursion that bounces back and forth between two functions is likely too much for the compiler to handle, especially when you might be returning different types through many paths.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you try to define a recursive function without a type, you will get this error: error: recursive value count needs type For example, note how this &#8220;count&#8221; value doesn&#8217;t have a type: val count = (values: Seq[Any]) => { values.headOption match { case Some(_) => 1 + count(values.tail) case None => 0 } } &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/fixing-scala-error-error-recursive-value-count-needs-type\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fixing scala error &#8220;error: recursive value count needs type&#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,488],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2669"}],"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=2669"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2669\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}