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.
Kotlin null safety actually eats up a considerable portion of cpu in runtime.
No. Just no.
[...] At runtime, objects of nullable types and objects of non-nullable types are treated the same: A nullable type isn't a wrapper for a non-nullable type. All checks are performed at compile time. That means there's almost no runtime overhead for working with nullable types in Kotlin.
Note: We say "almost" because, even though intrinsic checks are generated, their overhead is minimal.
View all comments
Show parent comment
32
u/HaMMeReD Jun 10 '24
Well, that's a bit of a gross oversimplification don't you think.
Like what if you want Null Safety? What if you don't like semicolons? Robust type inference?