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 KtDeclarationWithReturnType.returnType: KaTypeThe return type of the given
KtDeclarationWithReturnType.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. The entry point for the type building DSL is typeCreator accessible directly inside the analyze block.
Here's how you can build different types:
Building Class Types
To build a KaClassType, use the typeCreator.classType function. You can specify the target class either by its ClassId or using a KaClassLikeSymbol:
By a class ID:
By a class symbol:
Specifying Type Arguments:
Within the classType block, you can specify type arguments using the typeArgument function. This function accepts either a KaTypeProjection or a KaType and optionally its variance:
There is also invariantTypeArgument that allows adding a type argument with Variance.INVARIANT. If the type expects more type arguments than provided, the remaining ones are filled with KaStarTypeProjection (*). Excessive type arguments are discarded.
Nullability:
You can also set the nullability of the constructed type by modifying the isMarkedNullable property within the builder:
Building Array Types
If you'd like to build an array type, you can still use typeCreator.classType with the desired array ClassId. However, there is a more safe and convenient way to do it using typeCreator.arrayType:
By default, the builder constructs primitive array types when possible. E.g., for the Int element type, IntArray is constructed instead of Array<Int>. You can override this behavior by setting the shouldPreferPrimitiveTypes property within the builder to false.
Note: There is also a shortcut for constructing vararg array types, i.e., the underlying type of vararg function parameters with the given type. You can use typeCreator.varargArrayType for that:
Building Function Types
KaFunctionType can be constructed with typeCreator.functionType:
Specifying Return Type
The return type is defined by returnType. By default, it's set to Unit.
Specifying Parameters
The receiver type parameter can be set using receiverType. By default, it's set to null which indicates that the function is not an extension one.
Value parameters can be constructed using valueParameter. The API accepts an optional name and a type for the produced parameter:
The builder supports experimental context parameters as well. They can be added via contextParameter which accepts just a type:
Note: Kotlin prohibits context parameters in reflection types. All context parameters passed to the builder are discarded when isReflectType is set to true.
Creating Suspending and Reflection Function Types
typeCreator.functionType allows specifying whether the produces KaFunctionType is a suspending function type or a reflection function type. This is controlled by isSuspend and isReflectType respectively, the defaults are set to false.
Note: Kotlin prohibits context parameters in reflection types. All context parameters passed to the builder are discarded when isReflectType is set to true.
Building Type Parameter Types
To build a KaTypeParameterType, use the typeCreator.typeParameterType function. You need to provide the corresponding KaTypeParameterSymbol:
Note: Similar to class types, you can adjust the nullability of the type parameter type by using isMarkedNullable within the builder.
Building Definitely Not Null Types
KaDefinitelyNotNullType can be constructed using typeCreator.definitelyNotNullType by providing either a KaCapturedType or a KaTypeParameterType.
Note: If the original type was not nullable, typeCreator.definitelyNotNullType returns the original type as no additional wrapping is required.
By a captured type :
By a type parameter type:
Building Intersection Types
KaIntersectionType can be constructed using typeCreator.intersectionType by building a list of conjuncts via conjunct and conjuncts:
Note: The result of typeCreator.intersectionType is normalized, so it's not always a KaIntersectionType. However, the resulting type is guaranteed to represent a subtype of all the passed conjuncts.
Building Captured Types
KaCapturedType can be constructed using typeCreator.capturedType by providing either a KaTypeProjection or another KaCapturedType:
By a captured type:
By a type projection
Note: If the passed KaTypeProjection is a KaTypeArgumentWithVariance, its variance cannot be Variance.INVARIANT. That's because captured types are intended to capture non-invariant projections. Otherwise, an exception is thrown.
Building Flexible Types
KaFlexibleType can be constructed using typeCreator.flexibleType using three different ways.
If one of the provided bounds is a KaFlexibleType, the corresponding bound of this type is taken instead. E.g., if the upper bound is a flexible type itself, then its upper bound is taken instead.
If the lower bound is not a subtype of the upper bound, null is returned.
If bounds are equal, then it's unnecessary to create a flexible type for them, so this bound type is returned instead.
By another flexible type
By providing bounds manually
By providing bounds in the builder
Building Dynamic Types
KaDynamicType can be constructed using typeCreator.dynamicType:
Building Annotated Types
Each type except for KaIntersectionType can be constructed with additional annotations. To add annotations to your type, call annotation or annotations with desired annotation ClassIds within the builder.
Note: At the moment, only annotations without value arguments are supported. All annotations requiring arguments are discarded.
Building Type Projections
Building Type Arguments With Variance
KaTypeArgumentWithVariance can be constructed using typeCreator.typeProjection by providing a KaType along with its Variance:
Building Star Projections
KaStarTypeProjection (*) can be constructed using typeCreator.starTypeProjection: