Type checks and casts
In Kotlin, you can perform type checks to check the type of an object at runtime. Type casts enable you to convert objects to a different type.
is and !is operators
To perform a runtime check that identifies whether an object conforms to a given type, use the is
operator or its negated form !is
:
Smart casts
In most cases, you don't need to use explicit cast operators because the compiler automatically casts objects for you. This is called smart-casting. The compiler tracks the type checks and explicit casts for immutable values and inserts implicit (safe) casts automatically when necessary:
The compiler is even smart enough to know that a cast is safe if a negative check leads to a return:
Control flow
Smart casts work not only for if
conditional expressions but also for when
expressions and while
loops:
If you declare a variable of Boolean
type before using it in your if
, when
, or while
condition, then any information collected by the compiler about the variable will be accessible in the corresponding block for smart-casting.
This can be useful when you want to do things like extract boolean conditions into variables. Then, you can give the variable a meaningful name, which will improve your code readability and make it possible to reuse the variable later in your code. For example:
Logical operators
The compiler can perform smart casts on the right-hand side of &&
or ||
operators if there is a type check (regular or negative) on the left-hand side:
If you combine type checks for objects with an or
operator (||
), a smart cast is made to their closest common supertype:
Inline functions
The compiler can smart-cast variables captured within lambda functions that are passed to inline functions.
Inline functions are treated as having an implicit callsInPlace
contract. This means that any lambda functions passed to an inline function are called in place. Since lambda functions are called in place, the compiler knows that a lambda function can't leak references to any variables contained within its function body.
The compiler uses this knowledge, along with other analyses to decide whether it's safe to smart-cast any of the captured variables. For example:
Exception handling
Smart cast information is passed on to catch
and finally
blocks. This makes your code safer as the compiler tracks whether your object has a nullable type. For example:
Smart cast prerequisites
Smart casts can be used in the following conditions:
| Always, except local delegated properties. |
| If the property is |
| If the variable is not modified between the check and its usage, is not captured in a lambda that modifies it, and is not a local delegated property. |
| Never, because the variable can be modified at any time by other code. |
"Unsafe" cast operator
To explicitly cast an object to a non-nullable type, use the unsafe cast operator as
:
If the cast isn't possible, the compiler throws an exception. This is why it's called unsafe.
In the previous example, if y
is null
, the code above also throws an exception. This is because null
can't be cast to String
, as String
isn't nullable. To make the example work for possible null values, use a nullable type on the right-hand side of the cast:
"Safe" (nullable) cast operator
To avoid exceptions, use the safe cast operator as?
, which returns null
on failure.
Note that despite the fact that the right-hand side of as?
is a non-nullable type String
, the result of the cast is nullable.