title: Replacing SAM-constructor with lambda with covariant type
body: <p>I have got the following Java interfaces:</p>
  
  <pre><code>interface Action1&lt;T&gt; {
  
      void call(T t);
  }
  
  interface Test&lt;T&gt; {
  
      void test(Action1&lt;? super T&gt; action)
  }
  </code></pre>
  
  <p>And the following Kotlin class:</p>
  
  <pre><code>interface A {
      fun go()
  }
  
  abstract class Main {
  
      abstract fun a(): Test&lt;out A&gt;
  
      fun main() {
          a().test(Action1 { it.go() })
          a().test { it.go() }
      }
  }
  </code></pre>
  
  <p>Now in the function <code>main</code>, the first statement compiles, but IntelliJ gives a warning that the SAM-constructor can be replaced with a lambda.
  This would result in the second statement.</p>
  
  <p>However, this second statement does not compile, because <code>it</code> has type <code>Any?</code>, not <code>A</code>. Removing the <code>out</code> modifier makes it compile again.</p>
  
  <p>Why does this happen?</p>
  
  <hr>
  
  <p>The use case of this is when the implementing class of <code>Main</code> needs to return <code>Test&lt;B&gt;</code> for the function <code>a()</code>, where <code>B</code> implements <code>A</code>:</p>
  
  <pre><code>class B : A {
  
      override fun go() {
          TODO()
      }
  }
  
  class MainImp : Main() {
      override fun a(): Test&lt;out A&gt; {
          val value: Test&lt;B&gt; = object : Test&lt;B&gt; {
              override fun test(action: Action1&lt;in B&gt;?) {
                  TODO()
              }
          };
  
          return value
      }
  }
  </code></pre>