Marc Knaup
11/12/2022, 12:06 AMInterface needs to be a supertype of the class’s type parameter Implementation.Marc Knaup
11/12/2022, 12:06 AMinterface SomeInterface1
interface SomeInterface2
class SomeClass : SomeInterface1, SomeInterface2
class Component<Implementation> {
fun <Interface> provides(): Component<Implementation>
where Implementation : Interface // Implementation does not refer to a type parameter of provides
= this
}
fun <T> component(): Component<T> = TODO()
fun main() {
component<SomeClass>()
.provides<SomeInterface1>()
.provides<SomeInterface2>()
}Loney Chou
11/12/2022, 9:00 AMClass<Interface, Implementation : Interface>, or you have to check if Interface is the supertype of Implementation inside provide like this:
fun <Interface> provide(): Interface? {
return this as? Interface
}Youssef Shoaib [MOD]
11/12/2022, 10:14 AMinterface SomeInterface1
interface SomeInterface2
class SomeClass : SomeInterface1, SomeInterface2
class Component<Implementation> {
}
fun <Interface, Implementation> Component<Implementation>.provides(): Component<Implementation>
where Implementation : Interface
= this
fun <T> component(): Component<T> = TODO()
fun main() {
component<SomeClass>()
.provides<SomeInterface1, _>()
.provides<SomeInterface2, _>()
}
It's a little more clunky on the use-side, but it does compile!