https://kotlinlang.org logo
i

Ifvwm

06/28/2020, 2:12 PM
Copy code
interface IS<T> {fun <T>T.fmap():T}
sealed class IIS<T>: IS<T>
data class IntIS<Int>(val x:Int): IIS<Int>(){
    fun Int.fmap(){
        return x+1
    }
}
how to fix this?
d

deactivateduser

06/28/2020, 2:31 PM
what were you trying to achieve?
i

Ifvwm

06/28/2020, 2:35 PM
if a parameter declare has IIS<T>, so I can put a value has IntIS<Int> on there, and this value has fmap method, just like a typeclass
create a typeclass IS, and require to implement function fmap, then make Int and String as IS's instances, then where a parameter declare it has IS, then I can put Int or String value on there, and that value must have fmap function
generics constraints + interface + class extension
ad-hoc polymorphism
d

deactivateduser

06/28/2020, 2:43 PM
something like this? https://pl.kotl.in/u3IuTUInI... not really sure what is your use case... it looks overly complicated design...
k

Kroppeb

06/28/2020, 2:47 PM
i

Ifvwm

06/28/2020, 3:03 PM
why
return this
can't be
return x+1
in your code? there's (val x: Int) in the constractor
d

deactivateduser

06/28/2020, 3:25 PM
u can replace the
this
with
x+1
and the compiler will tell you why that one cannot be done... 😉.. basically, the returned type is different...
i

Ifvwm

06/28/2020, 3:28 PM
why? x declared as Int, and the function return Int, so why
return x+1
won't be Int?
k

Kroppeb

06/28/2020, 4:57 PM
What are you talking about?
I noticed a mistake from copying Handra's playground, as I was (and still am) on phone.
The issue was that you were shadowing the
Int
type