Hacker News new | past | comments | ask | show | jobs | submit login

Java's `final` while useful for primitives, it is not really that useful for reference types, which constitute the majority of types. C++'s `const` is a much better solution. Better yet, both Rust and D have superior immutability constructs.



Java's `final` while useful for primitives, it is not really that useful for reference types, which constitute the majority of types.

They are if you use them in conjunction with immutable classes, such as Guava's immutable collections.

Unfortunately, immutability is only a promise and not typed. But in practice, having a member that is final and of an immutable class works well.


Yep. The same applies with Scala too (with `val` vs `var` declarations). I still don't like the separation of type declarations with the actual implementation; they should be the same, which is what Rust and even C++ got right.


Well, with C++'s 'mutable' it's also not much more than a promise ;).


Immutability can be typed with Java 8's pluggable type systems . There's an implementation of immutable types in the Checker framework.


Sure, but at that point you're not really writing Java any more. If you decide to use Checkers many of Java's plus points (in particular the huge library ecosystem and all the development tools) no longer apply.


The entire JDK is annotated with Checkers types.


Sure, but what proportion of the packages in maven central are?


Well, all reference types are eventually made of primitives -- and arrays. The problem with Java's final is that there are no final arrays. This is planned to be resolved in Java 9.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: