Advanced reification of generic parameters

I have a class that takes two generic parameters. It is a custom class but for this discussion let’s think of it as Map<K, V> and for a specific instance let’s say I care about Map<Int, String>

I want to have an inline reified function that uses the K, V types in a reified manner. So I could do something like this where I have K, V as reified parameters as in:

inline fun <reified K, reified V> foo() {
   // Access K, V 

which I could call with foo<Int, String>() but I don’t want to pass the K, V values as separate parameters. What I want is to instead is make it so I call foo with a Map<K, V> as the single type parameter and it can access the K, and V in a reified manner. So what I really want is to be able to call foo like this:

    foo<Map<Int, String>>()

Why do I want to do this when it seems that it would be simpler to pass the two parameters and would be less typing? Well in my case the parameters are not short names like Int, and String and may be long names and I actually have type aliases for the Map type.

So in reality it is more like I have:

typealias MyType = Map<SomeLongTypeName, SomeOtherLongTypeName>

and I want to call it like this:


and not

    foo<SomeLongTypeName, SomeOtherLongTypeName>()

But I have not found any way to accomplish this. If this were an extension function it would be easy but I have no instance to call an extension function on.

Is there any way to accomplish this? I tried something like:

    inline fun <M: Map<K, V>, reified K, reified V> foo() { }

But then I have to pass 3 type parameters.

1 Like

It would be good if the _ character could be used for inference. So you would only type foo<MyTaype, _, _>() o.o

1 Like
inline fun <M: Map<K, V>, reified K, reified V> foo() { println("${K::class} ${V::class}") }
fun main() {
    foo<Map<Int, String>, _, _>()

A little bit ugly but it works