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.)