// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect // !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts // !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER import kotlin.contracts.* fun safeIsString(x: Any?): Boolean? { contract { returns(true) implies (x is String) } return x?.let { it is String } } fun elseWithNullableResult(x: Any?) { when (safeIsString(x)) { false -> x.length else -> x.length } when (safeIsString(x)) { true -> x.length else -> x.length } when (safeIsString(x)) { true -> x.length false -> x.length else -> x.length } when (safeIsString(x)) { true -> x.length null -> x.length else -> x.length } } fun exhaustiveWithNullableResult(x: Any?) { when (safeIsString(x)) { true -> x.length false -> x.length null -> x.length } when (safeIsString(x)) { false -> x.length true -> x.length null -> x.length } when (safeIsString(x)) { false -> x.length null -> x.length true -> x.length } }