Types
KaType is a fundamental concept in the Analysis API, representing types of Kotlin expressions and declarations. It provides information about the type structure, nullability, and annotations.
KaType Hierarchy
The KaType interface serves as the base for specific type kinds:
KaClassType: Represents types of classes, objects, and interfaces. This includes both named class types (e.g.,String,Int) and types of anonymous objects. For function types like(Int) -> String, there is aKaFunctionTypesubtype.KaTypeParameterType: Represents type parameter types, such asTinclass Box<T>().KaCapturedType: Represents captured types.KaDefinitelyNotNullType: Represents aT & Anytype.KaFlexibleType: Represents types with both a lower and upper bound, such asString?or(Mutable)List<Int>.KaIntersectionType: Represents types that are intersections of other types.KaDynamicType: Represents dynamic types, used for interoperability with dynamically typed languages (e.g., JavaScript).KaErrorType: Represents unresolved types, providing information about the error.
Example
The following example demonstrates how to obtain the KaType of a KtExpression and render its type:
Avoid using FqNames or raw strings for type comparison. Use ClassIds instead:
Getting a KaType
You have already seen how to get the expression type. Here is the complete list of utilities that convert either PSI or symbols to types:
val KtTypeReference.type: KaTypeA resolved reference type.
val KtExpression.expressionType: KaType?The expression type, or
nullif the given expression does not contribute a value.Particularly, the method returns:
A not-null type for valued expressions (e.g., a variable, a function call, a lambda expression);
Unitfor statements (e.g., assignments, loops);nullforKtExpressions that are not a part of the expression tree (e.g., expressions in import or package statements).
val PsiElement.expectedType: KaType?The expected type for the given element, or
nullif the element does not have an expected type.The expected type is the type that the expression is expected to have in the context where it appears.
val KtDeclaration.returnType: KaTypeThe return type of the given
KtDeclaration.Note: For
vararg foo: Tparameter returns fullArray<out T>type (unlikeKaValueParameterSymbol.returnType, which returnsT).val KtFunction.functionType: KaTypeThe functional type of the given
KtFunction.Depending on the function's attributes, such as
suspendor reflective access, different functional type, such asSuspendFunction,KFunction, orKSuspendFunction, will be constructed.val KaNamedClassSymbol.defaultType: KaTypeA class type where type parameters are substituted with matching type parameter types, e.g.
List<T>for theListclass.val KtDoubleColonExpression.receiverType: KaType?Receiver type of the
foo::barexpression, ornullif the expression is unresolved, or if the resolved callable reference is not of a reflection type.
Comparing KaTypes
Equivalence check
To check if two types are equivalent, use semanticallyEquals():
Comparing to type1 == type2, which only considers simple structural equivalence, semanticallyEquals() ensures that type1 can be substituted with type2.
Subtyping check
To determine if one type is a subtype of another, use isSubtypeOf:
Building KaTypes
The Analysis API provides facilities for constructing KaType instances, representing various Kotlin types. Here's how you can build different types:
Building Class Types
To build a class type, use the buildClassType function. You can specify the class either by its ClassId or using a KaClassLikeSymbol:
By a class name:
By a class symbol:
Specifying Type Arguments:
Within the buildClassType block, you can specify type arguments using the argument function. This function accepts either a KaTypeProjection or a KaType and optionally its variance:
Nullability:
You can also set the nullability of the constructed type by modifying the nullability property within the builder:
Building Type Parameter Types
To build a type parameter type, use the buildTypeParameterType function. You need to provide the corresponding KaTypeParameterSymbol:
Note: Similar to class types, you can adjust the nullability of the type parameter type within the builder.