title: Kotlin generic type inference failed
body: <p>I am translating some Scala code into Kotlin. This is based on code in the book <em>Functional Programming in Scala</em>. Here is a direct translation of the code into Kotlin:</p>
  
  <pre><code>data class State&lt;S, out A&gt;(val run: (S) -&gt; Pair&lt;A, S&gt;) {
      fun &lt;B&gt; flatMap(f: (A) -&gt; State&lt;S, B&gt;): State&lt;S, B&gt; = State { s -&gt;
          val (a, s1) = run(s)
          f(a).run(s1)
      }
  
      fun &lt;B&gt; map(f: (A) -&gt; B): State&lt;S, B&gt; =
              flatMap { a -&gt; unit(f(a)) }
  
      companion object {
          fun &lt;S, A&gt; unit(a: A): State&lt;S, A&gt; =
                  State { s -&gt; a to s }
      }
  }
  </code></pre>
  
  <p>The call to unit(f(a)) in map is causing the following error:</p>
  
  <pre><code>Error:(8, 28) Kotlin: Type inference failed: Not enough information to infer parameter S in 
      fun &lt;S, A&gt; unit(a: A): State&lt;S, A&gt;
  Please specify it explicitly.
  </code></pre>
  
  <p>Since the types are all generic, and it knows that it is the generic type S, I have no clue what type to specify. What is wrong with this code?</p>
  
  <p>Thank you in advance.</p>