Type infer in comparisons (equals, ...)

This code for example won’t compile:

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.

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.

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

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.

1 Like

exactly

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.