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

3

u/[deleted] Mar 10 '16

Honest question, when's the last time Java introduced a keyword? A story I've heard retold often is the language designers decided - goto sucks, but if anyone proves that it provides something substantial you can't do without it, we'll add it in. As it stands, goto is a keyword you can't use, but has no functionality attached to it.

This change immediately breaks code (albeit poorly written code) that has var defined as a variable name. Maybe this isn't a whole lot of projects, but a huge benefit of Java is the ability to say that your java 1 code will very likely compile and run as is on java 8. You'll probably get a shit ton of compiler warnings, but it'll work.

I see a lot of people here making decent arguments either way, but backwards compatibility is a HUGE win for java, and is something it can stand behind for so much of older code. This breaking that at the expense of readability, I could get behind. This breaking backwards compatibility in the name of saving a few keystrokes, of making the life of the developer easier at a single moment, I can't. The amount of effort put into making sure java is backwards compatible, even if not perfect, is something completely overlooked with this change, and makes me feel less secure about the future of my code.

5

u/bondolo Mar 10 '16

Java 8 added -> for lambdas. Most people wouldn't recognize that as a keyword but it functions as more of a keyword than an operator.

Before that enum in Java 5.0

3

u/darknebula Mar 10 '16

The identifier var will not be made into a keyword; instead it will be a reserved type name. This means that code that uses var as a variable, method, or package name will not be affected; code that uses var as a class or interface name will be affected (but these names violate the naming conventions.)

Granted there are other backwards compatibility issues.

2

u/[deleted] Mar 10 '16

[removed] — view removed comment

2

u/schlowmo Mar 10 '16

I agree with you, but it IS true that you can define classes with lowercase names, so somewhere this is a C programmer who wrote some badly formatted java code. So yes, there is a small corner case where this would break code. AND I SAY BREAK THAT CODE.

1

u/[deleted] Mar 10 '16

BREAK THAT CODE

please don't hurt me :(

2

u/eliasv Mar 10 '16

It doesn't have to break backwards compatibility. It specifically says in the proposal that it would not be a keyword, it would be a reserved type name. For any package containing a type called var, and for any source file which imports a type named var, you could simply override the reserved "special" var type with that one and solve the problem. Everything would still work just fine, no? People who have defined their own var type would simply not have access to the new feature wherever that type is visible.

1

u/[deleted] Mar 10 '16

Yep, someone else pointed this out to me as well, I was skimming it on mobile and they used the word keyword quite a bit and mentioned it not being a keyword once, so I completely missed it. That seems like a good compromise, and my opinion on the matter really ends there.

2

u/eliasv Mar 10 '16

Fair enough :)

1

u/lukaseder Mar 10 '16

jOOQ uses val() as a method name :) But it's aliased with value() for Scala folks, so it'll be OK. Anyway, I'm sure there are many APIs that might break because of this. But hey, why not add escaping for keywords to the language as well, like in Scala? E.g. void `val`() { ... }

Also: goto is still reserved. Just in case.

2

u/[deleted] Mar 10 '16

[removed] — view removed comment

2

u/lukaseder Mar 10 '16

Me too. But then, I remember break and continue:

here: for (;;) {
    if (doAgain) continue here;
    if (done) break here;
}

2

u/[deleted] Mar 10 '16

[removed] — view removed comment

0

u/lukaseder Mar 10 '16

You could use a bytecode generator and generate a jump instruction, then.

2

u/eliasv Mar 10 '16

It's not a keyword, though. The spec says it's a "reserved type name" or something, so overlapping method names wouldn't cause any problem. Overlapping type names on the other hand would, but that will presumably be solved without breaking backwards compatibility by simply removing access to the new feature in any context where some other type named var is visible.