Reified generics in interface

Hi, Is there a workaround that achieves the following:

interface Foo {
  inline fun <reified T> foo(thunk: () -> Any): Unit { }
}
4 Likes

This may come close:

interface Foo {
    fun foo(thunk: () -> Any, klass: KClass<*>)
}

inline fun <reified T> Foo.foo(noinline thunk: () -> Any) = foo(thunk, T::class)

But you’ll need to use klass to perform your casts, etc

3 Likes

Thanks for reply. I am trying to use the interface more as a trait, to provide some methods that can be mixed in to a class. So I want the users of the trait to be able to call foo { } directly from their class, so I don’t want the klass in the method signature that they use.

Can we expect a better fix in the future?

There is a KEEP for something that should help
https://github.com/47deg/KEEP/blob/master/proposals/type-classes.md

1 Like

Similar proposal using KClass