Skip to content

expressionType

context(session: KaSession)
val KtExpression.expressionType: KaType?

The type of the given KtExpression, or null if it does not have a type.

In particular:

  • A not-null type for valued expressions (e.g., a variable, a function call, a lambda expression).

  • Unit for statements (e.g., assignments, loops).

  • null for KtExpressions that are not a part of the expression tree (e.g., expressions in import or package statements).

Expression vs. expected type

The Analysis API distinguishes between an expression's type and its expected type, which represent different aspects of the Kotlin type system.

The expression type represents the actual type of an expression after it has been resolved. It reflects the result of type inference, smart casts, and implicit conversions.

The expected type represents the type that is expected for an expression at a specific location in the code. This is determined by the context in which the expression appears, such as a variable type for its initializer, or a parameter type for a function call.