Search code examples
genericsoperator-overloadingoperator-keywordkotlinjvm-languages

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


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


Solution

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