Safe function argument


The safe call operator is really handy when you have to work with nullable types. But sometimes you want to have the nullable type as a function argument which can’t be null, like this:


this doesn’t compile, because doSomething() requires a non-null parameter, so this is my usual workaround:

foo?.bar?.let { doSomething(it) }

but this gets really annoying in the long run

So I would like to have a syntax that basically does the same as above - return null when the parameter is null or else return the value of the function invocation, maybe like this:



I don’t know. Seems misleading, since it looks like the function call is working. If we did do something like this, I would rather it be that you add a question marker somewhere on the function call itself:


With my preference being on the first or last.

But, my true preference is to not have anything like this because null safety isn’t just supposed to help us have safe nulls, it’s supposed to help show us the pains of having null and try to find a way to avoid it.

Oh, also, you could make an extension method for bar that redirects to the original. Then you can call it like



but sometimes an extension makes semantically no sense, sometimes it’s already an extension function for another object, sometimes nulls are unavoidable. And what if the method has multiple of these parameters?

Yes I have grasped the pains of the null and try to avoid them as much as possible, but it is still a useful representation for “no value”

I do like your semantical suggestions. However, the first one would be unlikely since you could be calling the function on an (nullable) object (although that might still be possible), and the last one doesnt work because that space is reserved for further safe calls, that leaves us with the middle one as working.