Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Besides the 6 extra chars, the difference between “final var foo = ...” and “val foo = ...”?


I would argue it's not about the extra characters, it's about enforcing explicit mutability.

In Scala, where var/val both exist, each time you declare a variable you are forced to think about its mutability. But if you only have "var", with the _option_ of tacking "final" to it, then a programmer can simply forget to make that decision, because the language allowed them to.


Those "6 extra chars" mean more noise for human on the screen, it can lead to line wrapping or in other way harm the formatting, and last but not least it does not make it easier to promote good coding practices.


If the final keyword hadn't existed before I'd agree, but it does exist, does exactly what you'd expect it to here, and I'd argue that since it's consistent with how it used to work, is actually easier to understand and grasp. Local type inference is a new feature. Locals that can be assigned once is not and people are already familiar with how such locals are declared. And there's no ambiguity which variant is the correct one, e.g.:

    var i = 0;
    final var i = 0;
    int i = 0;
    final int i = 0;
    val i = 0;
One of the options there is a very odd one. And if there was "val", would "final var" be disallowed?


None of course, but I think that's the point of the complaint. For a feature supposed to increase convenience, it didn't go as far as it could have. Still, I'm glad var is in there. (Now, my employer just needs to get off of Java 8...)


be carful with adopting java9 and java10. they won't live that long...




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

Search: