Neal Gafter: Block Expressions For Java
(Via The Java Posse Google Group (Via Twitter))
Neal Gafter: A parenthesized expression can now contain additional statements, for example declarations of temporary local variables to avoid recomputing a value used only within the expression. This feature is especially useful in machine-generated code.
A simple example:
double pi2 = (double pi = Math.PI ; pi*pi);
This is comparable to the equivalent JavaFX Script:
var pi2 = {var pi = Math.PI; pi*pi};
The proposal sounds like it should be eligible for Project Coin.
But will the Java community accept language proposals from a Microsoft employee (where Neal now works.)?
Oh, wait. Java did it once already: auto-boxing, annotations, and enums.
Re: Neal Gafter: Block Expressions For Java
I guess I really don't understand the point of this.
I expect the compiler to notice that something's already been computed and optimize for me. That's its job.
As far as the "area of a circle" example goes, it looks cryptic. Someone who hasn't seen Java will not understand it. However they would likely understand
Randy
As far as the "area of a circle" example goes, it looks cryptic. Someone who hasn't seen Java will not understand it. However they would likely understand
double area(double radius)
{
return pi * radius * radius;
}
But then, I am old school,Randy
Block Expressions For Java
Even as a hard-core java programmer, I've always admired VB's "with" keyword using parenthesis. E.g.
StringBuilder sb = new StringBuilder();
with sb {
.append("Hello");
.append(" ");
.append("World");
}
It gives the Builder pattern to any object. It's just syntactic sugar.
Re: Neal Gafter: Block Expressions For Java
So, here is the "new way" followed by the "old way".
double pi2 = (double pi = Math.PI; pi*pi);
double pi2; {double pi = Math.PI; pi2 = pi*pi;}
The "new way" is 6 characters shorter!
It's not any more readable, it introduces a new unnecessary syntax, and the benefit described is to address performance. I'm very unconvinced. Could someone provide an example that might actually justify putting this in coin?
Re: Neal Gafter: Block Expressions For Java
No. That's the same right...
I understand the goal - I'm just concerned about eating up all of the expressiveness opportunities in the java syntax on trivial sugar. Closures was something much much more useful - maybe in Java8 - let's see that's how many years away now?
final double pi2 = (double pi = Math.PI; pi * pi);
final double pi2; { double pi = Math.PI; pi2 = pi * pi; }
You see you still get a final pi2. You can delay the assignment, you can only assign once.
I understand the goal - I'm just concerned about eating up all of the expressiveness opportunities in the java syntax on trivial sugar. Closures was something much much more useful - maybe in Java8 - let's see that's how many years away now?