# Type infer in comparisons (equals, ...)

**URL:** <https://discuss.kotlinlang.org/t/type-infer-in-comparisons-equals/27994>\
**Category:** Language Design\
**Created:** [January 22, 2024, 10:02am UTC](https://discuss.kotlinlang.org/t/type-infer-in-comparisons-equals/27994 "2024-01-22T10:02:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Nutz](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Alexander\_Nutz](https://discuss.kotlinlang.org/u/Alexander_Nutz)\
**Post date:** [January 22, 2024, 10:02am UTC](https://discuss.kotlinlang.org/t/type-infer-in-comparisons-equals/27994/1 "2024-01-22T10:02:08Z")

</div>

This code for example won’t compile:

```kotlin
if (msg.convert() == MSG_SCROLL) {

}

```

Why?  
`msg` is a `UInt`, `MSG_SCROLL` is a `Int`  
Now of course the solution seems obvious in this case BUT `MSG_SCROLL` is not guaranteed to be an `Int` (at compile time).  
`convert()` is a function in Kotlin native that takes a type argument, and it returns a value of that type. (This is not a Kotlin native only problem because the same problem exists for all functions everywhere)  
You would think that it just automatically infers the type. But no, it doesn’t. Instead I get an error that the type could not be inferred.  
I see no problem here why the compiler shouldn’t automatically infer the type.

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [January 22, 2024, 11:29am UTC](https://discuss.kotlinlang.org/t/type-infer-in-comparisons-equals/27994/2 "2024-01-22T11:29:18Z")

</div>

> [@Alexander\_Nutz](#):
>
> BUT `MSG_SCROLL` is not guaranteed to be an `Int` (at compile time).

Can you elaborate more on this point? I think this is your problem. Generic types are only generic at compile-time; types are erased at runtime, so type-inference only happens at compile-time. If your `MSG_SCROLL` is not guaranteed to be an `Int`, then the compiler’s type inference probably won’t work, depending on what type `MSG_SCROLL` could be.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [January 22, 2024, 1:47pm UTC](https://discuss.kotlinlang.org/t/type-infer-in-comparisons-equals/27994/3 "2024-01-22T13:47:35Z")

</div>

I don’t know Kotlin Native, but I guess the case is like this:

```kotlin
fun <T> Foo.convert(): T { ... }

```

If we use this function like this: `consumeInt(foo.convert())`, it will infer `T` to `Int`. But if we do `foo.convert() == 5` it won’t infer, even if it generally requires an int here.

---

<div class="post-metadata">

**Author:** ![Alexander\_Nutz](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Alexander\_Nutz](https://discuss.kotlinlang.org/u/Alexander_Nutz)\
**Post date:** [January 22, 2024, 2:02pm UTC](https://discuss.kotlinlang.org/t/type-infer-in-comparisons-equals/27994/4 "2024-01-22T14:02:01Z")

</div>

exactly

---

<div class="post-metadata">

**Author:** ![Alexander\_Nutz](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Alexander\_Nutz](https://discuss.kotlinlang.org/u/Alexander_Nutz)\
**Post date:** [January 22, 2024, 2:02pm UTC](https://discuss.kotlinlang.org/t/type-infer-in-comparisons-equals/27994/5 "2024-01-22T14:02:17Z")

</div>

> Now of course the solution seems obvious in this case BUT `MSG_SCROLL` is not guaranteed to be an `Int` (at compile time).

With `(at compile time)`, I meant that I know the type at compile time, but it might not always be the same type at compile time.
