katz
03/25/2024, 1:47 PMfoo?: return
== if(foo == null) return
• How about add inverse operator
foo !: return
foo !: return foo
foo !: return it
[it==foo captured value]
all above equals to if(foo!=null) return *
Case:
bafore
val errors = getErrors()
if(errors != null) return errors
we had to create val to hold result and then type if
after:
getErrors() !: return it
way simplierSam
03/25/2024, 1:52 PMlet
functionjw
03/25/2024, 2:57 PM?.return
which fits into existing semantics well. Of course you can already do ?. let { return it }
which isn't too bad, like Sam said. The inverse of ?:
is already essentially ?.
.katz
03/25/2024, 8:38 PMlet
is a good option, yet small improve may be good idea
?:
semantics and ?.
are too similar
In tearms of readablitity - !:
might be a bit more readable (acceptable) as !
and ?
are feels like opppositeskatz
03/25/2024, 8:44 PM!!:
is also an optionjw
03/25/2024, 8:45 PM!!
would surely throw NPE if it saw a null value.jw
03/25/2024, 8:47 PM?
is really nothing. You can see this in types String
vs. String?
and function calls foo.bar()
vs foo?.bar()
.jw
03/25/2024, 8:48 PM?:
which is weird syntax that breaks chaining. There's a decently upvoted issue which requests a functional version of it: https://youtrack.jetbrains.com/issue/KT-15962jw
03/25/2024, 8:51 PM?.
, so really thing the most logical thing to do (if we even agree there's a need to do anything) would be to advocate for ?.return
which should produce Nothing?
as its expression typekatz
03/26/2024, 3:27 PMsomeVar?.let { return(it) }
For single line checks might be for example somveFun()?.return
someFun()?.return(smth)
or similarkatz
03/26/2024, 3:29 PM?.let {return it}
migt be a bit more shorter