Skip to content

isUsedAsExpression

Whether the value of the given KtExpression is used. In other words, true if the value of the expression is not safe to discard.

isUsedAsExpression performs a conservative check instead of exhaustive control-flow analysis. For example, x in the following example is possibly used, even though the value is never consumed at runtime: x + try { throw Exception() } finally { return }.

Example

In the following examples, x is used as a value (x.isUsedAsExpression == true):

  • if (x) { ... } else { ... }

  • val a = x

  • x + 8

  • when (x) { 1 -> ...; else -> ... }

In these expressions, x is not used as a value (x.isUsedAsExpression == false)

  • run { x; println(50) }

  • when (x) { else -> ... }