Hey there, I have a weird generics issue where the...
# getting-started
s
Hey there, I have a weird generics issue where the order of the
where
clause produces some weird behavior on what the type of
box
ends up being when combined with a star projection (
Baz<*>
):
Copy code
interface Foo {
    fun isFoo(): Boolean
}
interface Bar{
    fun isBar(): Boolean
}
interface Baz<T> where T : Foo, T : Bar {
    val box: T
}
val baz: Baz<*> = TODO()
baz.box.isFoo() // OK where T : Foo, T : Bar -- NOT OK where T : Bar, T : Foo
baz.box.isBar() // NOT OK where T : Foo, T : Bar -- OK where T : Bar, T : Foo
Is this somehow intentional? Is there a workaround?
e
with
<*>
you are bypassing generic types - it is a raw type, not an "any type that works"
👍 1
JVM doesn't actually support intersection types, so both javac and kotlinc fake it by using the first type
nod 1
in other words, the interface looks like
Copy code
interface Baz {
    val box: Foo // or whichever one is mentioned first
}
when you remove the type machinery (that magically inserts
as T
when you have an actual
T
)
you can see this in the Java bytecode
s
Is this https://youtrack.jetbrains.com/issue/KT-7389? If so, it's fixed in Kotlin 2.0
s
Super interesting, makes sense!
That issue looks correct, maybe I can drag my feet until 2.0 release then 😆