# Set generics inference upper limit?

**URL:** https://discuss.kotlinlang.org/t/set-generics-inference-upper-limit/5545
**Category:** Support
**Created:** [November 24, 2017, 9:06am UTC](https://discuss.kotlinlang.org/t/set-generics-inference-upper-limit/5545 "2017-11-24T09:06:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![outring](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/outring/32/4081_2.png) [@outring](https://discuss.kotlinlang.org/u/outring)
#### Post date: [November 24, 2017, 9:06am UTC](https://discuss.kotlinlang.org/t/set-generics-inference-upper-limit/5545/1 "2017-11-24T09:06:45Z")

</div>

Consider the following function:

```auto
fun <T> doSomething(x1: T, x2: T) {
}

```

I expect, that both parameters will be of the same type not equal to `Any`. But when calling with totally different parameters, `Any` is being inferred and the following becomes totally legal:

```auto
doSomething("string", 0)

```

Is there any way to set an upper limit type or some other way to solve this within the single function?

---

<div class="post-metadata">

### Author: ![alanfo](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alanfo](https://discuss.kotlinlang.org/u/alanfo)
#### Post date: [November 24, 2017, 12:47pm UTC](https://discuss.kotlinlang.org/t/set-generics-inference-upper-limit/5545/2 "2017-11-24T12:47:38Z")

</div>

There is currently no way to constrain a type parameter so that multiple references to it all refer to the same type - in either Kotlin or Java.

However, you can cut down the possibilities by specifying that ‘T’ must inherit from another class (Number, perhaps, in your example) or must implement some interface.

So to prevent a String being assigned to x1 you could do:

```auto
fun <T : Number> doSomething(x1: T, x2: T) {}

```

I hope in due course other generic constraints will be added to the language though they probably won’t be unless they’re added to the JVM first or are restricted to inline functions.

---

<div class="post-metadata">

### Author: ![outring](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/outring/32/4081_2.png) [@outring](https://discuss.kotlinlang.org/u/outring)
#### Post date: [November 24, 2017, 3:27pm UTC](https://discuss.kotlinlang.org/t/set-generics-inference-upper-limit/5545/3 "2017-11-24T15:27:43Z")

</div>

Thanks, Alan. Unfortunately, I can’t set any constraint there ☹ I already thought about this option  
The only option, for now, is to implement some two-step builder-like solution (i.e. `with(x1).do(x2)`), not a single function call.

---

<div class="post-metadata">

### Author: ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)
#### Post date: [November 24, 2017, 3:53pm UTC](https://discuss.kotlinlang.org/t/set-generics-inference-upper-limit/5545/4 "2017-11-24T15:53:00Z")

</div>

> there any way to set an upper limit type or some other way to solve this within the single function?

No, there’s no such way at this moment.

See [https://youtrack.jetbrains.com/issue/KT-13198](https://youtrack.jetbrains.com/issue/KT-13198) for details. It would help if you could describe your concrete use case there.

---

<div class="post-metadata">

### Author: ![outring](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/outring/32/4081_2.png) [@outring](https://discuss.kotlinlang.org/u/outring)
#### Post date: [December 6, 2017, 7:13am UTC](https://discuss.kotlinlang.org/t/set-generics-inference-upper-limit/5545/5 "2017-12-06T07:13:32Z")

</div>

Thank you, Ilya. My case is already there! It’s also connected with KProperty1
