A while ago I was wondering if i had the ability t...
# language-proposals
g
A while ago I was wondering if i had the ability to disable type inference for particular generic methods. Somebody (I'm sorry I've forgotten) wondered what the use case would be. intersect is a strange method, and it could be used to mitigate its strangeness. If you have
Copy code
val x = listOf(1, 2, 3)
val y = listOf("a", "b", "c")

val c: List<Serializable> = x intersect y
which is legal kotlin code, and compiles. I think this should pretty clearly be a type error, with the compiler complaining with something like no suitable method found 'intersect' for P1<reciever>: List<Int>, and P2: List<String>" If kotlin had
intersect
as a regular extension function instead of an
infix fun
, with
@kotlin.internal.NoInfer
on its generic parameter, then the above code would indeed be a compiler failure, but it would be about how you need a type for
intersect
. This would either force you to select
Serializable
as your type --if thats actually what you meant, or, more likely, discover your error from a static compile time failure rather than a runtime failure! Of course, it also makes the code uglier.