createSubtypingUnificationSubstitutor¶
context(session: KaSession)
fun createSubtypingUnificationSubstitutor(leftTypesToRightTypes: List<Pair<KaType, KaType>>, isFreeTypeParameter: (KaTypeParameterSymbol) -> Boolean): KaSubstitutor?
Creates a KaSubstitutor which assigns type arguments such that, for each pair in leftTypesToRightTypes, the substituted left type is a subtype of the substituted right type. Returns null if such an assignment is not possible.
Note that when one type parameter is shared across several constraint pairs, all these pairs affect the resulting substitution for this parameter.
createSubtypingUnificationSubstitutor creates a constraint system, adds all the required bounds for 'leftType <: rightType' from each leftTypesToRightTypes pair and tries to solve the given constraint system:
-
If there were no contradictions found in the constraint system, the resulting substitutor is non-null. Otherwise,
nullis returned. -
If there are no type parameters involved in the provided types and every left type is a subtype of its right type,
KaSubstitutor.Emptyis returned. -
If
leftTypesToRightTypesis empty,KaSubstitutor.Emptyis returned as there can be no contradictions with no constraints.
isFreeTypeParameter is called on every type parameter involved in the provided types and controls the set of free type parameters registered in the constraint system. Only affects the construction when at least one of the involved types is generic, i.e., depends on a type parameter. The constraint system will only adjust the values of these free type parameters, and the produced substitutor will only contain mappings for these parameters.
context(session: KaSession)
fun createSubtypingUnificationSubstitutor(
leftType: KaType,
rightType: KaType,
constructionPolicy: KaUnificationSubstitutorPolicy
): KaSubstitutor?
Creates a KaSubstitutor which assigns type arguments such that the substituted leftType is a subtype of the substituted rightType. Returns null if such an assignment is not possible.
createSubtypingUnificationSubstitutor creates a constraint system, adds all the required bounds for 'leftType<: rightType' and tries to solve the given constraint system:
-
If there were no contradictions found in the constraint system, the resulting substitutor is non-null. Otherwise,
nullis returned. -
If there are no type parameters involved in the provided types and
leftTypeis a subtype ofrightType,KaSubstitutor.Emptyis returned.
constructionPolicy controls the way the unification substitutor is constructed. Only affects the construction when at least one of the involved types is generic, i.e., depends on a type parameter. See KaUnificationSubstitutorPolicy for more information and code examples.
Examples:¶
interface MyClass<A>
fun <T: X, X: R, R: Number> someFun(leftType: MyClass<Int>, rightType: MyClass<T>) {}
-
createSubtypingUnificationSubstitutor(MyClass<Int>, MyClass<T>, KaUnificationSubstitutorPolicy.ASSIGN_LEFT)returnsnull, asTis fixed and not guaranteed to be exactlyIntto satisfy the constraint. -
createSubtypingUnificationSubstitutor(MyClass<Int>, MyClass<T>, KaUnificationSubstitutorPolicy.ASSIGN_RIGHT)returnsKaSubstitutor { T -> kotlin/Int, X -> kotlin/Int, R -> kotlin/Int }. -
createSubtypingUnificationSubstitutor(MyClass<Int>, MyClass<T>, KaUnificationSubstitutorPolicy.ASSIGN_ALL)returns the exact same substitutorKaSubstitutor { T -> kotlin/Int, X -> kotlin/Int, R -> kotlin/Int }.
-
createSubtypingUnificationSubstitutor(List<C>, List<T>, KaUnificationSubstitutorPolicy.ASSIGN_LEFT)returnsKaSubstitutor { C -> T }. -
createSubtypingUnificationSubstitutor(List<C>, List<T>, KaUnificationSubstitutorPolicy.ASSIGN_RIGHT)returnsnull, asCis fixed and there is no assignment forTto satisfy the constraint (e.g., withC = kotlin/Any, whileTis bounded bykotlin/Int). -
createSubtypingUnificationSubstitutor(List<C>, List<T>, KaUnificationSubstitutorPolicy.ASSIGN_ALL)returnsKaSubstitutor { C -> kotlin/Int, T -> kotlin/Int }, as with such a substitution,List<C>is a subtype ofList<T>.
See also¶
context(session: KaSession)
fun createSubtypingUnificationSubstitutor(leftTypesToRightTypes: List<Pair<KaType, KaType>>, constructionPolicy: KaUnificationSubstitutorPolicy): KaSubstitutor?
Creates a KaSubstitutor which assigns type arguments such that, for each pair in leftTypesToRightTypes, the substituted left type is a subtype of the substituted right type. Returns null if such an assignment is not possible.
Note that when one type parameter is shared across several constraint pairs, all these pairs affect the resulting substitution for this parameter.
createSubtypingUnificationSubstitutor creates a constraint system, adds all the required bounds for 'leftType <: rightType' from each leftTypesToRightTypes pair and tries to solve the given constraint system:
-
If there were no contradictions found in the constraint system, the resulting substitutor is non-null. Otherwise,
nullis returned. -
If there are no type parameters involved in the provided types and every left type is a subtype of its right type,
KaSubstitutor.Emptyis returned. -
If
leftTypesToRightTypesis empty,KaSubstitutor.Emptyis returned as there can be no contradictions with no constraints.
constructionPolicy controls the way the unification substitutor is constructed. Only affects the construction when at least one of the involved types is generic, i.e., depends on a type parameter. See KaUnificationSubstitutorPolicy for more information and code examples.
Examples:¶
fun <X> rights(right1: List<X>, right2: List<X>) {}
fun lefts(left1: List<Int>, left2: List<String>) {}
-
createSubtypingUnificationSubstitutor(listOf(List<Int> to List<X>, List<String> to List<X>), KaUnificationSubstitutorPolicy.ASSIGN_LEFT)returnsnullas the left types contain no type parameters and the concrete left types are not subtypes of their generic right types. -
createSubtypingUnificationSubstitutor(listOf(List<Int> to List<X>, List<String> to List<X>), KaUnificationSubstitutorPolicy.ASSIGN_RIGHT)returnsKaSubstitutor { X -> intersection(kotlin/Comparable<*> & java/io/Serializable) }. -
createSubtypingUnificationSubstitutor(listOf(List<Int> to List<X>, List<String> to List<X>), KaUnificationSubstitutorPolicy.ASSIGN_ALL)returns the same substitutorKaSubstitutor { X -> intersection(kotlin/Comparable<*> & java/io/Serializable) }.