Skip to content

Instantly share code, notes, and snippets.

@Szer
Last active February 2, 2024 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Szer/17ce7c90727daa49e77785500aa60df1 to your computer and use it in GitHub Desktop.
Save Szer/17ce7c90727daa49e77785500aa60df1 to your computer and use it in GitHub Desktop.
package bug
import java.lang.reflect.ParameterizedType
abstract class Bar<T> {
val type: java.lang.reflect.Type
init {
val superClass = javaClass.genericSuperclass
type = (superClass as ParameterizedType).actualTypeArguments[0]
}
}
inline fun <reified T> wtf() =
object : Bar<T>() {}
inline fun <reified E> doesntWork() =
wtf<List<E>>().type
inline fun <reified E> works() =
(object : Bar<List<E>>() {}).type
fun main() {
println(doesntWork<String>().typeName)
println(works<String>().typeName)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment