Kotlin: Compiler doesn't infer generic type for some operators

603 Views Asked by At

Why can't Kotlin's compiler infer the generic type with some operator calls? For example:

class Example {

    operator inline fun <reified T : Any> unaryMinus(): T {
        ...
    }

}

Using it in a main method...

fun main(args: Array<String>) {
    val ex = Example()

    val works: Boolean = ex.unaryMinus()
    val doesntWork: Boolean = -ex
}

enter image description here

1

There are 1 best solutions below

0
Jire On BEST ANSWER

This is unfortunately the result of a bug: https://youtrack.jetbrains.com/issue/KT-10453