You can use it in combination with the Elvis operator. setText method to set the string I want to show, I either get an UnitializedPropertyException when I use lateinit or a NullPointer when I initialize it as null. Note that if the variable is a mutable property, you won't be able to smart cast it to its non-nullable type inside the if statement (because the value might have been modified by another thread) and you'd have to use the safe call operator with let instead. its supposed to be a dummy for different types of dialogs because Id prefer not to create a new one every time with a different xml for each one. The following example demonstrates its use cases. Thus let saves us from the if else null checker too Kotlin run Kotlin run is another interesting function. Therefore when comparing to null, the structural equality a = null is translated to a referential equality a = null.Īccording to the docs, there is no point in optimizing your code, so you can use a = null and a != null The code inside the let expression is executed only when the property is not null. A structural equality a = b is translated to a?.equals(b) ?: (b = null) The not null assertion () operator converts any value to a non-null type and throws an exception if the value is null.
0 Comments
Leave a Reply. |