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

Show parent comments

5

u/MrBIMC Mar 10 '16

9 is basically almost frozen already, especially for such a complex change.

4

u/cat_in_the_wall Mar 10 '16

Not trolling... why would this be complex? They already need to know the type of the right side of the expression, saying int x = 0.0 doesn't work, so they must already compare the lhs to the rhs. I would think that it would be fairly straight forward to defer the lhs type until after the rhs is known.

8

u/immibis Mar 10 '16

It's not complex to implement, but they'd have to consider all the weird cases, and write a section in the JLS for it, and probably check that other compilers can implement it reasonably, and check they're not shooting themself in the foot later.

It's also a new keyword.

(For an example of a weird case: x has what type here?

var x = () -> {System.out.println("foo");};

)

2

u/Tarmen Mar 10 '16

Been a while since I used java, why is that type ambiguous? Was it something like having to bind lambda expressions to an interface? Seems like inferring void=>void would be possible?

2

u/immibis Mar 10 '16

void=>void isn't a type in Java. It can be any interface that has a void=>void method - normally, which interface is deduced from the context you use it in (e.g. the type of the variable you assign it to).

2

u/_klg Mar 10 '16

In this case you would have to declare the type explicitly. This happens in C# as well, you can't assign an anonymous method to a var.

-1

u/mlk Mar 10 '16

Project Lombok already does this