Skip to content

Instantly share code, notes, and snippets.

Last active May 6, 2017 14:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
inline fun <reified T> typeToken(): TypeToken<T> = object : TypeToken<T>() { }
private val iterableParamType =[0]
private val mapKeyType =[0]
private val mapValueType =[1]
fun <T, I : Iterable<T>> TypeToken<I>.elementType(): TypeToken<T> {
return resolveType(iterableParamType) as TypeToken<T>
fun <K, M : Map<K, *>> TypeToken<M>.keyType(): TypeToken<K> {
return resolveType(mapKeyType) as TypeToken<K>
fun <V, M : Map<*, V>> TypeToken<M>.valueType(): TypeToken<V> {
return resolveType(mapValueType) as TypeToken<V>
fun main(args: Array<String>) {
val listType = typeToken<List<*>>()
val listElementType = listType.elementType()
val mapType = typeToken<Map<*, *>>()
val mapKeyType = mapType.keyType()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment