In kotlin, how to determine the correspondence between type parameters between subtypes and supertypes

已回答

Suppose I have a class like this

public class ParamMap<T> extends HashMap<String,T> {
}

I defined such a type in the code: ParamMap<String>, after I get its KotlinType instance, it must have a super type of Map<String, String>, but how do I know the corresponding relationship between the type parameter on ParamMap and the type parameter Map type, in this example, the T type parameter of ParamMap and the V type parameter of Map are corresponding, or inferred

0

You could use org.jetbrains.kotlin.types.TypeUtils#getAllSupertypes

For input such as `ParamMap<Int>`, it will return the set: `HashMap<String, Int>, Any...`

Then, it is simple to build such an association by mapping the type argument with the type constructor parameter.

 

However, please note that it is an old API, that works only with KotlinType (Which is K1-specific)

0

请先登录再写评论。