15

The code below will not compile because the variable myType can be null. Is there a way of executing a with block for nullable types in Kotlin?

    val myType: MyType? = null
    with(myType) {
        aMethodThatBelongsToMyType()
        anotherMemberMethod()            
    }
3
  • how about wrapping it in myType != null? Kotlin smart casts should handle it. Mar 1, 2016 at 12:53
  • 1
    Just a small remark: in Kotlin, with is not an operator but a function with two arguments of types T and T.() -> R, so its usage is the same to usage of any other function. See with documentation: kotlinlang.org/api/latest/jvm/stdlib/kotlin/with.html
    – hotkey
    Mar 1, 2016 at 13:51
  • Thanks, I updated the title to reflect your suggestion.
    – memoizr
    Mar 1, 2016 at 14:00

2 Answers 2

25

You can convert a nullable type to a non-nullable type with the suffix !!:

with(myType!!) {
    aMethodThatBelongsToMyType()
    anotherMemberMethod()            
}

If the value is indeed null, it will throw a NullPointerException, so this should generally be avoided.

A better way to do this is to make the execution of the code block dependent on the value being non-null by making a null-safe call and using the apply extension function instead of with:

myType?.apply {
    aMethodThatBelongsToMyType()
    anotherMemberMethod()            
}

Yet another option is to check if the value is non-null with an if statement. The compiler will insert a smart cast to a non-nullable type inside the if-block:

if (myType != null) {
    with(myType) {
        aMethodThatBelongsToMyType()
        anotherMemberMethod()            
    }
}
1
5

You could define your own with function that accepts nullables, then determines whether to actually run based on whether the object is null.

Like this:

fun <T, R> with(receiver: T?, block: T.() -> R): R? {
    return if(receiver == null) null else receiver.block()
}

Then you can call the code the way you wanted to in the example with no issues, and the result will equal null if what you pass in is null.

Or, if the code block should (and could) be run either way, even if myType is null, then you'd define it like this instead:

fun <T, R> with(receiver: T?, block: T?.() -> R): R {
    return receiver.block()
}
2
  • You could significantly improve the answer explaining how to do it (e.g. explain extension functions with nullable receiver).
    – hotkey
    Mar 1, 2016 at 13:53
  • Agreed, some sample code would definitely enrich this answer.
    – memoizr
    Mar 1, 2016 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.