I was on board of that train as an early Kotlin adopter but although I'm still a Kotlin fan I actually think that particular argument doesn't hold much water. Writing Java code in Kotlin's style is exactly as null safe as native Kotlin code is. The only nuance is a compiler error versus a static analysis error. And because Kotlin insists on being null safe you have to start working around it every time you work with null-first libraries (which is pretty much most of time in the real world). Your mileage may vary of course but I find myself writing idiomatic J21 code way more and Kotlin way less as the two converge.
Interesting, I find receiver lambdas incredibly anti-idiomatic. The whole point of a lambda is to be functional in nature and a receiver allows it to operate on the this instance. And all you're getting for it is slightly more concise code that's harder to parse visually. What are you getting out of it exactly?
I do not think Kotlin is a pure functional language. We overlay the object-oriented with streaming + functions. In the OO paradigm, this is an implicit input parameter with additional permission, etc. Lamda with the receiver is a dynamic runtime extension function. They could have used a better name that was not confused with the functional paradigm.
As for the use case, I created a cucumber-like testing framework with a single abstract class. I handed it off to a test engineer as an automated test tool. The specific use case is an electronic trading engine where it receives an order and sends out multiple orders to exchanges. I orchestrated various clients and numerous exchanges, which only took a few days. The engineer can easily extend it because it is code/Kotlin.
I don't either. I just think receiver lambdas hurt code quality (you can make a close to objective argument for it since it's literally a semi-documented side effect). To each their own of course.
Its a replacement of setting missing values to null no? Why is it not a good replacement of handling null values? I use it as a contract to indicate a return type may be missing
Yes, it's a contract for return types. That's about it. If used liberally, for example for parameters, it pollutes the api, and it still doesn't stop you from passing null to functions. It's simply insufficient. I'd encourage you to take a look at Kotlin null safety. You will quickly see how Optional is inferior.
The Streams API is so much easier to use than Kotlin's mature functional capabilities.
And it's wonderful to make every single value an Optional everywhere in your application rather than explicit nullable types that will allow the compiler to surface potential NPEs.
View all comments
Show parent comment
34
u/Mixabuben Jun 10 '24
There is no need to use Kotlin now, Java 17+ has everything you need