title: Type inference for higher order functions with generic return types
body: <p>The following example is perfectly legal in Kotlin 1.3.21:</p>
  
  <pre><code>fun &lt;T&gt; foo(bar: T): T = bar
  
  val t: Int = foo(1) // No need to declare foo&lt;Int&gt;(1) explicitly
  </code></pre>
  
  <p>But why doesn't type inference work for higher order functions?</p>
  
  <pre><code>fun &lt;T&gt; foo() = fun(bar: T): T = bar
  
  val t: Int = foo()(1) // Compile error: Type inference failed...
  </code></pre>
  
  <p>When using higher order functions,  Kotlin forces the call site to be:</p>
  
  <pre><code>val t = foo&lt;Int&gt;()(1)
  </code></pre>
  
  <p>Even if the return type of <code>foo</code> is specified explicitly, type inference still fails: </p>
  
  <pre><code>fun &lt;T&gt; foo(): (T) -&gt; T = fun(bar: T): T = bar
  
  val t: Int = foo()(1) // Compile error: Type inference failed...
  </code></pre>
  
  <p>However, when the generic type parameter is shared with the outer function, it works!</p>
  
  <pre><code>fun &lt;T&gt; foo(baz: T) = fun (bar: T): T = bar
  
  val t: Int = foo(1)(1) // Horray! But I want to write foo()(1) instead...
  </code></pre>
  
  <p>How do I write the function <code>foo</code> so that <code>foo()(1)</code> will compile, where <code>bar</code> is a generic type?</p>