Generics and Operators


#1

The following code will show an unresolved reference error for the `+`-sign in the editor. It lists a long list of candidates and the tooltip flickers when I hover my cursor over the `+`-sign.

data class Var<T>(var value: T)

fun plusOp<Int>(var1: Var<Int>,
           var2: Var<Int>): Var<Int> {
  return Var(var1.value + var2.value)
}


I wanted to overload the plus method for Var. Instead I created plusOp(), because the editor somehow mixed Int.plus() with Var.plus().

Using &lt;T: Number&gt; is not an option, because Number in Kotlin does not implement plus(). &lt;T: Int&gt; says, that Int is final (and I don’t need a generic for this method).

What to do about the unrevolved reference error?

–Benjamin


#2

Kotlin plugin: 0.13.870 Kotlin compiler: 0.1-SNAPSHOT (manifest.mf says 0.13.870)


#3

This seems to work. However, as already said, the compiler outputs the warning "kotlin.Int is a final type...".

fun plusOp<T: Int>(var1: Var<T>,                    var2: Var<T>): Var<Int> {   return Var(var1.value + var2.value) }


#4

It's a bug, I created issue https://youtrack.jetbrains.com/issue/KT-9018


#5

I looked over my source code once again.

fun plusOp<Int>(...) {}

This will create a new generic variable called Int, and has nothing to do with kotlin.Int. This code part remained from my attempts to use `<T: Number>` in the generic.

However the flickering and screen filling toolkit is irritating.


#6

Oh, yes, if remove it all is right)