# Reified type parameters and type inference

**URL:** <https://discuss.kotlinlang.org/t/reified-type-parameters-and-type-inference/1397>\
**Category:** Language Design\
**Created:** [January 4, 2016, 9:51pm UTC](https://discuss.kotlinlang.org/t/reified-type-parameters-and-type-inference/1397 "2016-01-04T21:51:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pniederw1](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pniederw1](https://discuss.kotlinlang.org/u/pniederw1)\
**Post date:** [January 4, 2016, 9:51pm UTC](https://discuss.kotlinlang.org/t/reified-type-parameters-and-type-inference/1397/1 "2016-01-04T21:51:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [January 18, 2016, 12:54pm UTC](https://discuss.kotlinlang.org/t/reified-type-parameters-and-type-inference/1397/2 "2016-01-18T12:54:33Z")

</div>

> [@pniederw1](#):
>
> Is there a way for me or the language to improve on this?

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

> [@pniederw1](#):
>
> 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?

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.

> [@pniederw1](#):
>
> 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.)

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