I'm using the typeclassless style of DI via interf...
# arrow
j
I'm using the typeclassless style of DI via interface composition.
Copy code
class G
class H

interface A<T> {
    fun T.foo(t: T): T
}

interface B : A<G>

interface C : A<H>

interface E : B, C // Compiler error: Type parameter T of 'A' has inconsistent values: InterfaceComposition.G, InterfaceComposition.H
Is there a way to get around the compiler error? My use case is that A is a generic handler of events of type T. So B and C are specific handler implementations for the events G and H. I would like to be able to do:
Copy code
val a: A = TODO()
when (a) {
   is B, is C -> a.foo(..)
   else -> TODO()
}
Without a common base interface, I'd have to do:
Copy code
val a: A = TODO()
when (a) {
   is B -> a.foo(..)
   is C -> a.foo(..)
   else -> TODO()
}
Thanks!
k
when you get an
val a: A<T>
, the foo function could be called as long as
T
is in receiver scope. ie:
Copy code
val t: T = TODO()
with(t) { a.foo() }
I am not sure why you want to match on specific type, or why you have different B and C interface.
k
You can't, it's a limitation of the JVM
j
Thanks @Kroppeb
What's the second-best way to do this, given the limitation of the JVM?