Kotlin generics


#1

Hello every body.
I want to do some thing like this

data class TestGen<T: Number>(var x : T, var y : T)

public operator<T:Number> fun Int.plus(p:TestGen<T>) = TestGen(this+p.x, p.y)

so how i can do it? or any other idea to do the same thing?


#2

For example

operator fun <T : Number> Int.plus(p: TestGen<T>): TestGen<Int> = 
    TestGen(this + p.x.toInt(), p.y.toInt())

#3

thanks @Beholder but i want to do this for all other operators plus , minus, multi … etc and for Int, Float and Double .
so when i do this , i will do it for all these types


#4

You can’t do this completely generic. If you want to do this, you have to create pretty much duplicate methods for each number type.


#5

Thanks @Jonathan.Haas and @Beholder
I have got a solution in stackoverflow , you can see in this link