

For example, we can call our let2() in this way: let2(v1, v2) ).isNull()Īs the assertions show, the letIfAnyNotNull() function will pack all not-null values in a list and pass the list to our lambda expression. In Kotlin, if the last parameter of a function is a function, we can pass a lambda expression to it and put it outside of (…). We execute the block(p1, p2) function only if both p1 and p2 are not null. The function block decides the return type of the let2() function. block is a function that accepts two not-null arguments and returns a nullable R instance.Īs we can see in the code above, let2() is a generic function, so the two parameters’ ( p1 and p2) types can be different.

p1 and p2 are the two nullable parameters.

As an example, the following expression is illegal in Kotlin. For example, Nullable string can be declared by. To assign a null value to a variable, you must declare a nullable variable type by adding to the end of the base type. To make a reference as null, kotlin has separate reference type for each of the type known as Nullable type. The function let2() has three parameters: p1, p2, and block. In Kotlin, references to objects cannot contain null values by default.
