# Static/companion generic constraints?

**URL:** <https://discuss.kotlinlang.org/t/static-companion-generic-constraints/12961>\
**Category:** Uncategorized\
**Created:** [June 5, 2019, 10:00am UTC](https://discuss.kotlinlang.org/t/static-companion-generic-constraints/12961 "2019-06-05T10:00:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rnikander](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnikander/32/6881_2.png) [@rnikander](https://discuss.kotlinlang.org/u/rnikander)\
**Post date:** [June 5, 2019, 10:00am UTC](https://discuss.kotlinlang.org/t/static-companion-generic-constraints/12961/1 "2019-06-05T10:00:12Z")

</div>

Is there a way to constrain type parameters like this? In this case I’m using kotlinx serialization, so want to say that the type `T` has the given static/companion method to get the serializer.

```
fun <T> HttpServletResponse.respondSerializable(x: T) {
    val serializer = T.serializer() // ???
    val jsonString = Json.stringify(serializer, x)
    this.writer.write(jsonString)
}

```

---

<div class="post-metadata">

**Author:** ![Alexey.Belkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexey.belkov/32/5215_2.png) [@Alexey.Belkov](https://discuss.kotlinlang.org/u/Alexey.Belkov)\
**Post date:** [June 10, 2019, 12:47pm UTC](https://discuss.kotlinlang.org/t/static-companion-generic-constraints/12961/2 "2019-06-10T12:47:15Z")

</div>

Hello. Kotlin can’t constrain type parameters like that. Error message for reference: `[TYPE_PARAMETER_ON_LHS_OF_DOT] Type parameter 'T' cannot have or inherit a companion object, so it cannot be on the left hand side of dot`.

You’re welcome to submit an issue at [http://kotl.in/issue](http://kotl.in/issue) describing your use case. If it turns out that this is mainly needed for serialization, maybe this could be supported as a special case in the serialization compiler plugin.

---

<div class="post-metadata">

**Author:** ![rnikander](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnikander/32/6881_2.png) [@rnikander](https://discuss.kotlinlang.org/u/rnikander)\
**Post date:** [June 11, 2019, 6:54am UTC](https://discuss.kotlinlang.org/t/static-companion-generic-constraints/12961/3 "2019-06-11T06:54:38Z")

</div>

Thanks. I was trying to understand the language, but I don’t have a strong use case yet. If I find one I will of course file an issue to let you know.
