Here\'s a simple generic function in Kotlin:
fun twice(x: T) : T { return 2 * x }
Attempting to build this (either in a project or RE
Since T
could be anything, the compiler is not able to find a matching times
operator. As you can see in the error message, for Int
, there a multiple alternatives available
public final operator fun times(other: Byte): Int defined in kotlin.Int
public final operator fun times(other: Double): Double defined in kotlin.Int
public final operator fun times(other: Float): Float defined in kotlin.Int
public final operator fun times(other: Int): Int defined in kotlin.Int
public final operator fun times(other: Long): Long defined in kotlin.Int
public final operator fun times(other: Short): Int defined in kotlin.Int
But unfortunately there's no generic times
function, which can be used with e.g. Number
. I'm afraid in this case, you would have to create an overload for each type you want to be handling, i.e., Double
, Int
, etc.