# Is there a more succinct way to achieve 'inline fun \<reified T\>'?

**URL:** <https://discuss.kotlinlang.org/t/is-there-a-more-succinct-way-to-achieve-inline-fun-reified-t/18637>\
**Category:** Language Design\
**Created:** [July 30, 2020, 10:31pm UTC](https://discuss.kotlinlang.org/t/is-there-a-more-succinct-way-to-achieve-inline-fun-reified-t/18637 "2020-07-30T22:31:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rollie42](https://avatars.discourse-cdn.com/v4/letter/r/7ba0ec/32.png) [@rollie42](https://discuss.kotlinlang.org/u/rollie42)\
**Post date:** [July 30, 2020, 10:31pm UTC](https://discuss.kotlinlang.org/t/is-there-a-more-succinct-way-to-achieve-inline-fun-reified-t/18637/1 "2020-07-30T22:31:40Z")

</div>

After creating a handful of functions for some task, some of which have generic parameters, I eventually call a ktor function `httpClient.get<T>(someUrl)`. But, in order for this to compile, the calling function must have a reified type param, which implies it must also be inline. Okay, no problem; but then _that_ functions calling function must be inline with reified type param. And _its_ calling function, and so on. To top it off, it also implies previously private functions can no longer be private if called by non-private functions, so my interfaces goes from

```
fun <T> A()
fun <T> B()
private fun <T> C()

```

to

```
inline fun <reified T> A()
inline fun <reified T> B()
inline fun <reified T> C()

```

Much less pretty, IMO. Is there no way to have syntactic sugar for this (automatic reification perhaps?), such that suddenly needing to call a fun with reified type param doesn’t necessitate changing every function in the hierarchy? It feels very non-user friendly in an otherwise very user friendly language.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [July 30, 2020, 11:13pm UTC](https://discuss.kotlinlang.org/t/is-there-a-more-succinct-way-to-achieve-inline-fun-reified-t/18637/2 "2020-07-30T23:13:55Z")

</div>

Not really. Automatic reification sounds like a nice idea but as soon as you realize the downsides it’s impossible.  
The issue is that reification relies on inlining of functions which means no inheritance and no recursion. This is not something that the compiler should automatically force on the programmer. Also inlining can greatly increase the size of the generated bytecode which is another downside.

I never used ktor so I can’t really comment on that part, but I never read anything about long callchains of inlined functions, so maybe it’s a problem with the design of your system and not ktor. But as I said, I never used ktor myself so maybe someone else can help you with that part. I doubt that changing all functions to reified inline functions is a viable solution to your problem though.

---

<div class="post-metadata">

**Author:** ![rollie42](https://avatars.discourse-cdn.com/v4/letter/r/7ba0ec/32.png) [@rollie42](https://discuss.kotlinlang.org/u/rollie42)\
**Post date:** [July 30, 2020, 11:38pm UTC](https://discuss.kotlinlang.org/t/is-there-a-more-succinct-way-to-achieve-inline-fun-reified-t/18637/3 "2020-07-30T23:38:24Z")

</div>

Understood; I can imagine auto-reification would work often, but when it breaks would not be fun to debug. ktor isn’t the meat of this topic though; I think the deserialization of HTTP responses relies on annotations on the type, which are lost due to type erasure. I guess the other option is to examine the object at runtime and fill in parameters that correspond to the values in the response json, but that approach is not encouraged due to performance impact.

Conceptually, any `fun deserializeSomeJson<T>(json: String)` knows that it needs type information of T; if it is able somehow to express that in it’s compiled signature, code using this function could pass in the relevant type/annotation information automatically. Maybe something like that isn’t possible; I’m more trying to express the problem than say I have a good solution 🙂

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [July 31, 2020, 9:37am UTC](https://discuss.kotlinlang.org/t/is-there-a-more-succinct-way-to-achieve-inline-fun-reified-t/18637/4 "2020-07-31T09:37:28Z")

</div>

I’ve got no experience with Ktor whatsoever, but you could probably look at how `get<>()` is implemented and copy that implementation into a regular function that takes a `KClass<>` parameter (because pretty much everything that you can do with a reified type parameter you can do with a KClass). You can also create a small wrapper over `A()` that uses a reified type instead  
With that, now you can have:

```auto
inline fun <reified T> A(){
    A(T::class)
}
fun <T> A(c: KClass<T>)
fun <T> B(c: KClass<T>)
private fun <T> C(c: KClass<T>){
    ...
    httpClient.get(someUrl, c)
}

```

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [July 31, 2020, 12:22pm UTC](https://discuss.kotlinlang.org/t/is-there-a-more-succinct-way-to-achieve-inline-fun-reified-t/18637/5 "2020-07-31T12:22:24Z")

</div>

I proposed this some time ago…

> [@Typeclass KClass](https://discuss.kotlinlang.org/t/typeclass-kclass/6760):
>
> Hi, this proposal follow the typeclass on Kotlin. The class kotlin.reflect.KClass can become a typeclass, ie: typeclass KClass\<T : Any\> For each exsisting type T the Kotlin compiler is already able to define a specific instance of KClass\<T\>, currenlty it is expressed as T::class. The goal is to mimic a reified type without requiring the inlining. A simple example is: class TypePredicate\<T\> : Predicate\<T\> given KClass\<T\> { override fun filter(e : T) = e is T } val stringTypePredicate …

It looks pretty complicated, but it may work for your use case.  
In practice, you don’t need to define the function as inline, but you can get the type parameter automagically.

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [July 31, 2020, 3:08pm UTC](https://discuss.kotlinlang.org/t/is-there-a-more-succinct-way-to-achieve-inline-fun-reified-t/18637/6 "2020-07-31T15:08:17Z")

</div>

TypeClasses in general would be great for Kotlin. I think the folks who work on Arrow have actually made a compiler plugin that adds them
