9

I have got the following Java interfaces:

interface Action1<T> {

    void call(T t);
}

interface Test<T> {

    void test(Action1<? super T> action)
}

And the following Kotlin class:

interface A {
    fun go()
}

abstract class Main {

    abstract fun a(): Test<out A>

    fun main() {
        a().test(Action1 { it.go() })
        a().test { it.go() }
    }
}

Now in the function main, 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.

However, this second statement does not compile, because it has type Any?, not A. Removing the out modifier makes it compile again.

Why does this happen?


The use case of this is when the implementing class of Main needs to return Test<B> for the function a(), where B implements A:

class B : A {

    override fun go() {
        TODO()
    }
}

class MainImp : Main() {
    override fun a(): Test<out A> {
        val value: Test<B> = object : Test<B> {
            override fun test(action: Action1<in B>?) {
                TODO()
            }
        };

        return value
    }
}

1 Answer 1

1

It is a compiler bug. You can track it here: https://youtrack.jetbrains.com/issue/KT-12238.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.