You can define your own with function, which takes on nullable values, and then determines whether it will actually execute 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 name the code as you wanted in the example, no problem, and the result will be null if what you pass is null .
Or, if the code block should (and could) be executed anyway, even if myType is null , you should define it like this:
fun <T, R> with(receiver: T?, block: T?.() -> R): R { return receiver.block() }
source share