<< JavaFX 1.1 Released: Still No Linux Version | Home | Another Month, Another Messaging Stack Goes Open Source... >>

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

That's a pretty small piece of sugar that might limit options in the future... After all it's not much more than contracting this valid java:

    double pi2;
    {
      double pi = Math.PI;
      pi2 = pi*pi;
    }

to this:
    double pi2; { double pi = Math.PI; pi2 = pi * pi; }

Re: Neal Gafter: Block Expressions For Java

This seems like inline block expressions only and not blocks that you can pass around, right? The syntax looks OK, though doesn't include an example of parameter passing into the block. The implicit return is good.

Re: Neal Gafter: Block Expressions For Java

But, Mario, This is Java, where we literally debate new language features to death.

Having said that, I think something like the following might work:

{double => double} area = (double r => double pi = Math.PI; pi*r*r);

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
double area(double radius)
{
   return pi * radius * radius;
}
But then, I am old school,
Randy

Re: Neal Gafter: Block Expressions For Java

Anyone else than me getting tired of reading about new language and productivity features only to get disappointed? Certain existing languages (C#) and upcoming ones (Fan) looks more and more appealing.

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

Agree with anonymous above me. It's rather pointless. If you could manipulate the thing 'functionally' ala Scala... that'd be a different story...

Re: Neal Gafter: Block Expressions For Java

There is a very clear advantage beside a few characters:
final double pi2 = (double pi = Math.PI; pi*pi);
But I don't like it anyway, it only goes half-way. I wouldn't want this without also:
final boolean b = if (x) {...} else if (y) {...} else {...}

Re: Neal Gafter: Block Expressions For Java

No. That's the same right...
     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?

Re: Neal Gafter: Block Expressions For Java

This is programming by convention, the convention being return the last value calculated. Java does not use programming by convention. We need to be explicit about the return value.
Summary: This is not in the spirit of Java.

Re: Neal Gafter: Block Expressions For Java

I am also inclined to agree that this is unnecessary sugar and is not the way Java is intended to do things.

Add a comment Send a TrackBack