r/programming • • Mar 09 '16

var keyword proposed for Java

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

395 comments sorted by

View all comments

88

u/omair-majid Mar 09 '16

They are holding a survey on keywords (var, var/val, var/let): https://www.surveymonkey.com/r/KGPTHCG

Bikeshed away!

40

u/oscarreyes Mar 09 '16

I would love to see var / final

var   a = expr; 
final b = 1;

31

u/sudowork Mar 10 '16

Var/val from Scala is pretty reasonable as well.

16

u/kcuf Mar 10 '16

And already used by JVM developers that mix languages (I believe Kotlin uses this as well as Scala).

→ More replies (1)

11

u/FrozenCow Mar 10 '16

Not a big pro, but it also aligns nicely.

7

u/kukiric Mar 10 '16

Though the one-character difference between the two keywords might cause some confusion about whether a local is final or not, and it would break existing code that uses any variables named "val" (as undescriptive as it is, it's still pretty common). "var/let" would be more reasonable for these reasons, IMO.

7

u/orclev Mar 10 '16

That's why I voted for var/let, I like that they're easier to tell apart. I feel like var/val would be just a bit too easy to typo.

→ More replies (5)

6

u/mirhagk Mar 10 '16

it would break existing code that uses any variables named "val"

Would it? Are all keywords in java reserved and unusable as variable names? (var would be problematic too as well). When C# introduced var there was no compatibility issues as var wasn't valid anywhere where variable names were valid, and if a type in scope happened to be named var (which would go against the naming conventions anyways) that type would simply be used instead of the keyword. In C# the following program compiles perfectly fine before and after the introduction of var

void Main()
{
	var var = new var();
	var.num = 4;
}

// Define other methods and classes here
class var
{
	public int num{get;set;}
}

and using int var = 5; is likewise perfectly fine. If you don't have a var type then var var = "var"; creates a variable named var that is type inferred as a string. I don't know if the Java team wants to create this context sensitive complex grammar, but if they are willing to then it can be completely backwards compatible.

3

u/gbear605 Mar 10 '16

Just checked with Java keyword usability as variable names.

If I do

int int = 5;

it errors with

[javac] file.java:67: error: not a statement

[javac] 			int int = 5;

[javac] 			^

[javac] file.java:67: error: ';' expected

[javac] 			int int = 5;

[javac] 			   ^

[javac] file.java:67: error: not a statement

[javac] 			int int = 5;

[javac] 			    ^

[javac] file.java:67: error: ';' expected

[javac] 			int int = 5;
→ More replies (1)
→ More replies (1)

0

u/[deleted] Mar 09 '16

[deleted]

11

u/immibis Mar 10 '16

The parser would become slightly more complicated to deal with cases like

final String = 5;
             ^ apparent parse error?

(In Java it is legal to have a variable with the same name as a type)

→ More replies (8)

18

u/[deleted] Mar 09 '16

[removed] — view removed comment

15

u/RobIII Mar 10 '16

I wish C# have final or something equivalent for local variables.

Watch this one: Proposal: 'readonly' for Locals and Parameters

→ More replies (4)
→ More replies (11)

8

u/Jacoby6000 Mar 10 '16

Why not var for mutable things, and Val for immutable things?

0

u/[deleted] Mar 10 '16

[deleted]

6

u/demon_ix Mar 10 '16

var/val are already used in this way in other languages (scala, kotlin and more). They also have the benefit of being the same length, which is nice.

6

u/mirhagk Mar 10 '16

As much as I'm voting for var/val in C# I do think Java would benefit from just doing final since everyone already knows that keyword and what it means in Java.

→ More replies (1)

3

u/ComradeGibbon Mar 10 '16

That's pretty nice actually. val/Val/value is immutable. var is mutable.

value quarter_pi = 3.14/4.0;
var foo = bar->baz->foo;
foo += quarter_pi;
→ More replies (6)

19

u/danneu Mar 09 '16

really the immutable reference should be the shortest one to type so that it's default. too late for java, of course. this is something i think rust got right. let vs mut let.

javascript fucked it up too. let vs const.

8

u/Ununoctium117 Mar 10 '16

It's let mut, not mut let fyi :D

→ More replies (10)

4

u/irarandomdude Mar 10 '16

Just use var for non-final and val for final like everyone else does.

→ More replies (1)

2

u/plastikmissile Mar 09 '16

Is "final" declaring an immutable?

11

u/TheNiXXeD Mar 09 '16

Final already exists. It's not about immutability, but you can't reassign that reference.

16

u/rabidcow Mar 09 '16

It is about immutability, but shallow immutability. Since primitives are the only values types, that means just the reference for anything else.

2

u/id2bi Mar 10 '16

Or say that we are talking about the (im)mutability of variables, not values.

I'm not sure talking about "shallow immutability" is appropriate in this context. When you do a shallow copy, only the fields will be copied (i.e. copies of primitives, and copies of references), so this suggests that shallow immutability means the fields will also not be re-assignable.

This is also backed up by this:

https://homes.cs.washington.edu/~mernst/pubs/immutability-aliasing-2013-lncs7850.pdf

→ More replies (1)
→ More replies (1)
→ More replies (2)
→ More replies (4)