by kodablah · 8y ago · 0 comments
I want something in between Kotlin and Scala. I want option as a real type that is treated as a max-single-element collection that can be flattened with the same APIs as other collections. But I want no runtime penalty. People in Rust are so lucky to have zero cost abstractions for these things. I suppose I'll need better compile time support (even more than Scala macros) and whole program optimizations (i.e. cross JAR) to get zero cost optional on the JVM. Things like Scala Native use LLVM and surely Option things are inlined or otherwise optimized out.
There was an experiment to use it as a replacement for the implementation of `scala.Option` in the dotty compiler code base [2], but it is inconclusive so far; it should be tried directly in the collections library.
I think it's impressive† that you got nesting to work, I'm really curious how you pulled that off for longs and doubles without incurring extra overhead.
† Though, given the magic you worked in getting Union types working in the ScalaJS facades I shouldn't be surprised.
And in that implementation, `None.toString == "None"`, `Some(None).toString == "Some(None)", etc. Although that could be changed.
If you think my unboxed option is not monadic, please provide a counter-example to one of the monad laws.
Instead you just get a crash if it is null which is worse in my opinion.
> And all of the null checks would have muddied up code and added (admittedly minimal) runtime costs.
This already happens automatically for parameters to a Kotlin function; check out the Kotlin Intrinsics checks.
Only in the same instances you would in Java-to-Java. So the ergonomics aren't improved or reduced. And really, this only helps on returns anyways. Marking every Java param nullable gets you nothing if the implementer didn't handle it well.
> This already happens automatically for parameters to a Kotlin function; check out the Kotlin Intrinsics checks.
Yup, and I don't like it. So even public Kotlin-to-Kotlin calls suffer. Haven't checked in a while, but I would like an option to use annotations only and skip those top-of-method checks.
This isn't correct. For example you have a String that was passed as a param and were shoving it into JSONObject. In Java if it's null nothing bad happens (except shoving a null value into your JSON). Shoving a null into a JSONObject in Kotlin won't crash in the put operation; you'd get the crash as soon as the method is called and it does the Intrinsics check.
I've measured the null-checks and other people have too... the runtime performance cost is negligible, so disabling them would get you nothing useful. If they did, Kotlin would have made the compiler flag to disable them (which exists) public... but until someone comes up with an actual reason other than "I feel like it's better" that won't change.
I'm tired of being surprised by the bytecode that Kotlin generates. You could argue javac has some magic too, but not near as much and it's pretty well spelled out whereas you won't find the docs for all of these intrinsics.
It's even worse when things just get dismissed as "I feel like it's better" and then when you provide reasons people will say it's not normal. Languages (and their proponents) should not treat the end user (i.e. the dev) like they are stupid, or at least give them the option to turn things off. The trade-off of hiding this stuff is not worth it...it's more like it's the language people doing the "I feel like it's better".
I don't want the bytecodes added. I have tracing I am doing and I need more predictable instructions. It's not just a performance issue.
Also, you should not assume cost of a JVM implementation of things based on your empirical evidence from a single, popular VM. For example, how does TeaVM translate it?
0 - https://stackoverflow.com/questions/43129365/javas-math-rint...
Reading up on the format [1] it seems that every value is defined, which includes a ton of possible NaN values [2].
[1] https://en.wikipedia.org/wiki/Double-precision_floating-poin...
[2] If you read the actual IEEE 754 spec, one suggested use for a NaN value is the address of the offending instruction(s); otherwise there is no defined pattern for NaNs in IEEE-754.
> there is no defined pattern for NaNs
?
I was referring to using a NaN value without a zeroed mantissa.
Not sure which of these kotlin has if any
1. declaring that a type isn’t null with ! appended.
2. Suppression annotations beatable at the file / class / function / line levels to disable the check
3. Facades / Decorators that can be added by anyone that tell the compiler retrofitted additional typing information on existing libraries (the defintely typed / typescript approach)