Reified type parameters and type inference


#1

I keep running into the following situation:

I want to use a reified type parameter for a method, due to its advantages (for users and implementors) over a (K)Class method parameter. However, the method requires a second, non-reified type parameter, and forcing users to always specify that second parameter is not an option (it can always be inferred). Since Kotlin doesn’t allow to specify just the reified type parameter, I have no choice but to replace it with a method parameter of type (K)Class, despite the drawbacks (no flow typing for instanceof-checks, ::class(.java) ugliness, etc.).

Is there a way for me or the language to improve on this?

Related question #1: Could an instanceof-check against (K)Class<T> be considered in flow-typing? That is, could type of x be T after if (clazz.isInstance(x))? If this is not feasible for Class.isInstance, could a similar method be added to KClass?

Related question #2: Could an automatic KClass->Class transformation be supported for method arguments, like it is for annotations? This would reduce the ::class.java ugliness to ::class when dealing with Java APIs. (I guess the answer is “no”, but I thought I’d ask.)


#2

Best you can do ATM is use some fluent interfaces to split your type parameters between two calls, but it’s rather bulky

That would tie up Kotlin’s type checker to particular library function. Unless we find a generic way doing this for arbitrary functions, we refrain from such things in the language.

It boils down to “no”, right. We try to avoid implicit conversions as much as we can.