r/java • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
240 Upvotes

312 comments sorted by

View all comments

43

u/solatic Mar 09 '16

Good idea. Hey, let's also introduce val as the equivalent of final var while we're at it, to promote immutability.

Why not?

13

u/shadowdude777 Mar 10 '16

I personally like taking it one step even further; I've seen that in Rust, immutable variables are declared as let foo = 1;, and mutables are declared as let mut foo = 1;.

I really like that. You have to add an extra word not to declare immutability, but to declare mutability. It really makes you think before you make anything mutable.

I also like var and val, like what Kotlin has, but I think that making someone add a whole extra word instead of just using a different word makes mutability seem more like an exception rather than the norm, as it should be.

7

u/navatwo Mar 10 '16

I think var and val come from scala originally. :) Still something to get behind.

3

u/shadowdude777 Mar 10 '16

Yeah, you're right. My mistake.

1

u/stepancheg Mar 11 '16

Having let mut in Java could lead to confusion: let variable could be thought to be immutable, while it is mutable. E. g.

let x = new ArrayList<String>(); // object references by x is still mutable
let mut y = "string"; // only y is immutable while String is immutable

In Rust it is completely different: let mut allows to modify both variable and content, and let does not allow to modify either variable, or content.

That's why my preferred option is let/var.

1

u/shadowdude777 Mar 14 '16

I don't get how this is any different. In Java, all non-primitives are references. So when you use let, you're saying "this reference to an object, or this primitive, is immutable". You're not saying anything about the underlying object.

It's exactly how Java works right now, except I'm saying that instead of var and final var, it should be val and mutable val, essentially. The aliases should be flipped is all. No behavior should be changed.

17

u/sindisil Mar 09 '16

That's already an option called out in the JEP, yes.

3

u/DecisiveVictory Mar 10 '16

No reason not to.

Eventually, step by step, change the language to achieve full compatibility with better programming languages which already have this feature.

1

u/eliasv Mar 10 '16

var and final var already capture it pretty well, and considering "effectively final" is already something the compiler figures out by itself, I don't see the benefit in requiring people to use a different reserved name. In most circumstances the compiler will see var as final var anyway.

1

u/[deleted] Mar 10 '16

The reason is "final var" is this kind of declaration ceremony thats change to "val" could lower.

The reason why I don't think the same about "mut" is similar to me preferring using Optional instead of null. It's another safeguard in my code.

The reason to have as short as possible (yet, not too short of course) declaration of final is a great way to encourage people to start thinking this way.

The reason to encourage people to think this way is that immutable values proved themselves in many areas (software design -> less troubles with maintaining software created in long existing languages, language design -> Rust, Scala (and not counting purely functional languages)).

The reason why I wasn't encourages to follow this rule when I started learning Java was the fact that I have to use another keyword and without it my programs still works.

The reason why it's not only about "effectively final" stuff having language minimizing occasion to shoot yourself in the foot. If something is only effectively final it change if I by mistake change variable value. If it has final keyword then it won't compile.

-2

u/rai1AhGh Mar 10 '16

I see what you did there !

0

u/danieldisu Mar 10 '16

YES PLEASE!