A few suggestions for small language changes
Ron Pressler
ron at paralleluniverse.co
Fri Jan 9 17:12:42 UTC 2015
Hi everyone!
In the spirit of Java 7's project Coin, I'd like to propose four small
changes to the Java language. They require no new keywords or symbols, and
are fully backwards compatible. More importantly, they add no complexity --
in fact, they remove it, as a new Java programmer might expect the language
to behave in this way, and be surprised that it doesn't. None of the
proposed changes relies on any other.
1. Local declarations in conditionals and expressions:
if ((int a = foo()) > 0 && (String b = bar()) != null)
return b + a;
(vs:
int a = foo();
if (a > 0) {
String b = bar();
if (b != null)
return b + a;
}
or:
int a;
String b;
if ((a = foo()) > 0 && (b = bar()) != null)
return b + a;
)
and:
return (int a = foo()) > 0 && (String b = bar()) != null ? b + a : "";
The scope of the variables is the expression or the if/while block.
This change will reduce nesting and LOC, or eliminate empty declarations
that are removed from initializations, and will reduce scope pollution.
This change will make Doug Lea's code shorter and a bit easier to follow :)
2. Allow no return from Void method:
@Override
Void foo() {
System.out.println("done");
}
This makes Void behave more like void.
3. Allow returning void (and use in expressions):
@Override
Void foo() {
return System.out.println("done");
}
but also:
void foo() {
return System.out.println("done");
}
and:
return a > b ? System.out.println("X") : System.exit(0);
This makes void behave more like Void ("void autoboxing")
4. Smart auto-casting:
Object x = foo();
if (x instanceof String)
return x.length();
Obviously, this reduces a lot of tedious casts that the compiler can do on
its own. This is a more significant change, and there's the question of
handling conjunction and disjunction (or nesting) of instanceof tests.
This can also be taken further to introduce some sort of a switch over the
runtime type of a variable (a poor man's pattern matching), but that would
require new syntax.
Ron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20150109/8930aadc/attachment.html>
More information about the compiler-dev
mailing list