{"id":13274,"date":"2017-12-04T09:20:08","date_gmt":"2017-12-04T08:20:08","guid":{"rendered":"https:\/\/touk.pl\/blog\/?p=13274"},"modified":"2017-12-04T09:20:08","modified_gmt":"2017-12-04T08:20:08","slug":"kotlin-type-inference-puzzler","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2017\/12\/04\/kotlin-type-inference-puzzler\/","title":{"rendered":"Kotlin Type-Inference Puzzler"},"content":{"rendered":"<p><a href=\"https:\/\/kotlinlang.org\">Kotlin<\/a> takes Type-Inference to the next level (at least in comparison to Java) which is great, but there&#8217;re scenarios, in which it can backfire on us.<\/p>\n<p><!--more--><\/p>\n<h2 id=\"the-riddle\">The Riddle<\/h2>\n<pre>fun foo(int: Int?) = {\n    println(int)\n}\n\nfun main(args: Array&lt;String&gt;) {\n    listOf(42).forEach { foo(it) }\n}<\/pre>\n<p>And the question is: <strong>what does the <em>puzzler()<\/em> method print and why it&#8217;s not 42?<\/strong> Assume that the <em>main()<\/em> method gets executed.<\/p>\n<p>The answer can be found at the very end of the article.<\/p>\n<h2 id=\"explanation\">Explanation<\/h2>\n<p>In Kotlin, we&#8217;ve got not only a local variable type inference (which is also <a href=\"http:\/\/openjdk.java.net\/jeps\/286\">coming to Java<\/a>) but also the return value type inference when writing single-expression methods.<\/p>\n<p>Which means that if we write a method:<\/p>\n<pre>fun foo() : String  {\n    return \"42\"\n}<\/pre>\n<p>We can rewrite it using the single-expression method syntax and ignore the explicit type declaration and the <em>return<\/em> keyword:<\/p>\n<pre>fun foo() = \"42\"<\/pre>\n<p>So, if we have a method:<\/p>\n<pre>fun foo(int: Int?) : Unit {\n    println(int)\n    return Unit\n}<\/pre>\n<p>we could get rid of the return type declaration, as well as the explicit <em>return<\/em> statement by leveraging single-expression method syntax, right? but we already know that the following doesn&#8217;t work:<\/p>\n<pre>fun foo(int: Int?) = {\n    println(int)\n}<\/pre>\n<p>The devil&#8217;s in the details but everything becomes crystal clear if we specify the return type explicitly:<\/p>\n<pre>fun foo(int: Int?) : () -&gt; Unit = {\n    println(int)\n}<\/pre>\n<p>If we look closely, it can be seen that we mixed two approaches here and actually defined a method that doesn&#8217;t return <em>Unit<\/em> but a <em>() -&gt; Unit<\/em> itself &#8211; which is simply an action that doesn&#8217;t accept any parameters and doesn&#8217;t return <em>anything<\/em> &#8211; which is semantically equivalent to returning a <em>java.lang.Runnable<\/em> instance.<\/p>\n<p>This is because Kotlin utilizes curly braces not only for defining classes\/methods but also for defining Lambda Expressions and\u00a0<em>{ println(42) }<\/em> is a valid Lambda Expression declaration:<\/p>\n<pre>val foo: () -&gt; Unit = { println(42) }<\/pre>\n<p>So, our original example is simply a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Higher-order_function\">Higher-Order Function<\/a> accepting an <em>Int<\/em> parameter and returning a function that prints it &#8211; when we return it in the <em>forEach()<\/em>\u00a0the return value just gets ignored.<\/p>\n<p>So, if we want to fix our example, we have two ways to go.<\/p>\n<p>Explicitly call invoke() on the returned function:<\/p>\n<pre>listOf(42)\n  .forEach { foo(it).invoke() }<\/pre>\n<p>or simply define the method properly by removing the &#8220;<em>=<\/em>&#8221; sign:<\/p>\n<pre>fun foo(int: Int?) {\n    println(int)\n}<\/pre>\n<p>or by leveraging the single-expression method syntax:<\/p>\n<pre>fun foo(int: Int?) = println(int)<\/pre>\n<p>That&#8217;s why I encourage my fellow team members to declare return types explicitly.<\/p>\n<p>Code snippets <a href=\"https:\/\/github.com\/pivovarit\/articles\/tree\/master\/kotlin-type-inference\">can be found on GitHub.<\/a><\/p>\n<h3 id=\"answer\">Answer<\/h3>\n<p>The method prints nothing.<\/p>\n","protected":false},"excerpt":{"rendered":"Kotlin takes Type-Inference to the next level (at least in comparison to Java) which is great, but there&#8217;re&hellip;\n","protected":false},"author":68,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":{"0":"post-13274","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=13274"}],"version-history":[{"count":1,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13274\/revisions"}],"predecessor-version":[{"id":13275,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13274\/revisions\/13275"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=13274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=13274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=13274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}