You could use these arguments against a lot of syntax decisions in Kotlin (e.g the safe casting as? vs as).
As far as I understand Kotlin aims to be a concise language which helps you handle nullable types gracefuly, so “close to optimal” should not be the stopping point.
I really like this point and I agree the same argument should be applied to other Kotlin features such as the safe-cast operator
The argument is that the cost of adding the feature is not worth the minor optimization gained (I especially love the minus 100 points rule)
I suspect it would be difficult to demonstrate enough use for a return? value operator as a shortcut for “if not null, return it” for a few reasons:
Too few use cases
Most use cases could be refactored (IMHO, maybe something cleaner with a return at the end?)
The current solutions are not problematic enough to warrant a change
Maybe enough use cases can be presented that are also poorly satisfied with the current methods to change this? Until then I’d suggest this feature isn’t a high priority.
I actually dig this syntax.
Saying something is close to being optimal can be said on java syntax as well from a java developer’s point of view. and still, Kotlin developers decided to change some parts of the java syntax to be more concise and clear.
The change seems pretty simple to understand as a new concept and can make this use case (as specific as it is) more concise and clear.
The let solution is the same as the if statement solution in a different form IMHO, this proposal looks far more in the “Kotlin” way.
I actually dislike all these tricks. If there is a branch, I want to see it right away, with a quick glance, without looking into detail. I don’t even like
if (value != null) return value
I’d rather have
if (value != null)
return value
This way, the branch is clearly visible. As an additional bonus, I can put a breakpoint on the return line. And the code is so clean and readable so I see no reason at all to do anything about it. It reads as plain English: if value is not equal to null, return it.
You make like it or not, but implicit branches are already in Kotlin and used everywhere. Every ? character denotes an implicit branch. So it seems only logical to embrace it even more. I like return? suggestion and extending it to break?, continue? would not hurt either.
Fair enough. But is it consistent? Is it “return if not null” or “return if null“? How am I to remember that value ?: return means “return if null“ and that return? value means “return value if not null“? Makes no sense to me.
If we really need such a feature (and I’m not sure we do), then I’d rather have value.return as a synonym for return value and value?.return for the proposed feature. It’s more OOP-like and consistent with the ?. usage. Or make it value?.return() instead, but I don’t really like the idea of a flow control operator looking suspiciously like a function call.
And if anyone thinks that value?.return looks weird, think of a popular value ?: return idiom. You can even format it as value ?. return for further consistency. ?. stand for “if not null, then…” and ?: stands for “if it IS null, then…“. Makes perfect sense to me.
If I see a return? I’ll immediately assume it means return if null, even though it wouldn’t make much sense to check and return only if the value is null.