Lucy Poulton
11/13/2023, 3:41 PMinterface First {
fun foo()
}
interface Second {
fun bar()
}
data class FirstAndSecond<T>(val value: T) where T : First, T : Second
fun main() {
val thing: FirstAndSecond<*> = null!! // pretend there's a real instance here
thing.value.foo()
thing.value.bar() // Unresolved reference: bar
}
I would expect thing.value
here to implement both First and Second as per the type constraint, but it's only ever using the first type constraint (interestingly the first defined one takes priority, if I swap them around I can access bar() but not foo()). Am I misunderstanding how this works, or have I run into a Kotlin limitation?Sam
11/13/2023, 3:58 PMSam
11/13/2023, 3:59 PMfun <T> fooBar(thing: T) where T : First, T : Second {
thing.foo()
thing.bar()
}
Lucy Poulton
11/13/2023, 3:59 PMKlitos Kyriacou
11/13/2023, 4:08 PM(thing.value as First).foo()
(thing.value as Second).bar()