# Multiple generic constraints not resolved with star-projection

**URL:** <https://discuss.kotlinlang.org/t/multiple-generic-constraints-not-resolved-with-star-projection/8207>\
**Category:** Language Design\
**Created:** [June 19, 2018, 2:28pm UTC](https://discuss.kotlinlang.org/t/multiple-generic-constraints-not-resolved-with-star-projection/8207 "2018-06-19T14:28:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![knolle](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@knolle](https://discuss.kotlinlang.org/u/knolle)\
**Post date:** [June 19, 2018, 2:28pm UTC](https://discuss.kotlinlang.org/t/multiple-generic-constraints-not-resolved-with-star-projection/8207/1 "2018-06-19T14:28:41Z")

</div>

When declaring multiple generic constraints on a type with `where` and then using the type with star-projection, I expect the upper bound to be a combination of all the types in the constraint.  
However, it turns out, that only the first declared bound is recognized there.  
If you switch `where T : A, T : B` to `where T : B, T : A`, the compile error also moves to the a-usage 🙂  
See my example…

```
class GenericConstraintsTest {

    interface A {
        val a: String
    }

    open class B(val b: String)

    class C(override val a: String, b: String) : B(b), A

    open class D<T>(val d: T)
        where T : A, T : B

    fun foo(x: D<*>) {
        x.d.a
        x.d.b // compile error 

        // the below works!
        val y = D(C("a", "b"))
        y.d.a
        y.d.b
    }
}

```

Is this a bug?

---

<div class="post-metadata">

**Author:** ![knolle](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@knolle](https://discuss.kotlinlang.org/u/knolle)\
**Post date:** [June 20, 2018, 8:52am UTC](https://discuss.kotlinlang.org/t/multiple-generic-constraints-not-resolved-with-star-projection/8207/2 "2018-06-20T08:52:26Z")

</div>

Well, I created [https://youtrack.jetbrains.com/issue/KT-25007](https://youtrack.jetbrains.com/issue/KT-25007)

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [June 20, 2018, 11:19am UTC](https://discuss.kotlinlang.org/t/multiple-generic-constraints-not-resolved-with-star-projection/8207/3 "2018-06-20T11:19:21Z")

</div>

I think the problem has something to do with this:

> […] Only one upper bound can be specified inside the angle brackets. If the same type parameter needs more than one upper bound, we need a separate where-clause
> 
> See: [https://kotlinlang.org/docs/reference/generics.html#upper-bounds](https://kotlinlang.org/docs/reference/generics.html#upper-bounds)

Seeing that explanation I can deduct that the JVM can only store one upper bound, so when you specify more than one using a **`where`** clause, **only the first one is _stored_**. This can be tested by changing the order of the upper bounds in the where clause:

#### First `T:A` then `T:B`

```run-kotlin
class GenericConstraintsTest {
    interface A {
        val a: String
    }

    open class B(val b: String)

    class C(override val a: String, b: String) : B(b), A

    //sampleStart
    open class D<T>(val d: T)
            where T : A, T : B // Only 'T:A' is taken into account

    fun foo(x: D<*>) {
        x.d.a
        x.d.b // compile error
        //sampleEnd

        // the below works!
        val y = D(C("a", "b"))
        y.d.a
        y.d.b
    }
}

```

#### First `T:B` then `T:A`

```run-kotlin
class GenericConstraintsTest {
    interface A {
        val a: String
    }

    open class B(val b: String)

    class C(override val a: String, b: String) : B(b), A
    
    //sampleStart
    open class D<T>(val d: T)
            where T : B, T : A // Only 'T:B' is taken into account

    fun foo(x: D<*>) {
        x.d.a // compile error
        x.d.b
        //sampleEnd
        
        // the below works!
        val y = D(C("a", "b"))
        y.d.a
        y.d.b
    }
}

```

**Note** : Click the play button to see the compilation error in the kotlin playground.

**Disclaimer** : I have no technical knowledge about the JVM or how it works, this is all based on some test I have done. Maybe I am completely wrong, or maybe not. 😅
