Skip to content

isDefinitelyNull

Whether this expression is definitely null, based on the declared nullability and smart cast types derived from data-flow analysis facts.

Only nullability from stable smart casts is considered. See the smart cast sink stability section of the Kotlin specification for more information.

Examples

public fun <T : Any> foo(t: T, nt: T?, s: String, ns: String?) {
  t     // t.isDefinitelyNull()  == false && t.isDefinitelyNotNull()  == true
  nt    // nt.isDefinitelyNull() == false && nt.isDefinitelyNotNull() == false
  s     // s.isDefinitelyNull()  == false && s.isDefinitelyNotNull()  == true
  ns    // ns.isDefinitelyNull() == false && ns.isDefinitelyNotNull() == false

  if (ns != null) {
    ns  // ns.isDefinitelyNull() == false && ns.isDefinitelyNotNull() == true
  } else {
    ns  // ns.isDefinitelyNull() == true  && ns.isDefinitelyNotNull() == false
  }

  ns!!  // From this point on: ns.isDefinitelyNull() == false && ns.isDefinitelyNotNull() == true
}