Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The ?. (safe call) operator is a null-safety mechanism in Kotlin that performs conditional member access. It evaluates the receiver expression and, if the receiver is non-null, executes the property access or function call. If the receiver evaluates to null, the operator short-circuits the evaluation and immediately returns null without throwing a NullPointerException.

Syntax and Evaluation

The operator is placed directly between the nullable receiver and the member (property or function) being accessed.
val result = receiver?.member
Under the hood, the Kotlin compiler translates this into a conditional check. The semantic equivalent of the safe call operator is:
val result = if (receiver != null) receiver.member else null

Type System Implications

The safe call operator alters the inferred return type of the expression by forcing it to be nullable, regardless of the member’s original return type. If receiver.member resolves to a non-nullable type T, the expression receiver?.member will resolve to the nullable type T?.
val text: String? = "Kotlin"
// length is of type Int, but safeLength is inferred as Int?
val safeLength: Int? = text?.length 

Short-Circuiting in Chains

When multiple safe call operators are chained sequentially, the expression evaluates from left to right. The first null encountered in the chain immediately halts further evaluation, short-circuiting the rest of the expression and yielding null for the entire chain.
val result = objectA?.propertyB?.propertyC?.methodD()
In this execution flow:
  1. If objectA is null, propertyB, propertyC, and methodD() are never evaluated.
  2. If objectA is non-null but propertyB is null, the chain halts before accessing propertyC.

Short-Circuiting in Assignments

When the safe call operator is used on the left-hand side of an assignment, the short-circuiting behavior extends to the right-hand side expression. If the receiver is null, the assignment is skipped entirely, and the expression on the right is never evaluated.
// computeValue() is never executed if receiver is null
receiver?.property = computeValue() 
Master Kotlin with Deep Grasping Methodology!Learn More