As you know, JavaFX Script programs consist of declarative expressions to define the UI, create instances of classes, etc. Compiled JavaFX Script will expand its use of expressions, in that it will be an expression language, with block expressions. To appreciate the elegance of this feature, check out this excerpt from one of the docs (in its current form) from the OpenJFX Compiler Project:
<snip>
Expressions
Unlike Java, JavaFX script is an expression language. All executable statements are expressions which consist of zero or more inputs and a single output. This includes conditionals, loops, and even blocks. For example:
import java.lang.Math;
var rand = (Math.random() * 100).intValue();
var s:String = null;
if (rand % 2 == 0) {
s = "rand is eve";
} else {
s = "rand is odd";
}
System.out.println(s);
In the above example the then and else clauses of the conditional "if" are expressions in their own right, namely block expressions.
Block Expression
A block expression consists of a list of expressions surrounded by curly braces and separated by semicolons. The value of a block expression is the value of its last expression. So, the above example could also be written like this, in JavaFX script:
import java.lang.Math;
var rand = (Math.random() * 100).intValue();
var s:String =
if (rand % 2 == 0) {
"rand is even";
} else {
"rand is odd";
};
System.out.println(s);
Or, alternatively the braces can be omitted:
import java.lang.Math;
var rand = (Math.random() * 100).intValue();
var s:String = if (rand % 2 == 0) "rand is even" else "rand is odd";
System.out.println(s);
In Java, we have both an "if" statement, and a conditional expression, e.g, a < b ? a : b
. As you can see, thanks to block expressions, the JavaFX "if" expression takes the place of both.
</snip>
Some Background Info
If you're interested in the perspective of one of the developers (Robert Field) on the Compiled JavaFX Script team as this decision was forming, read a September 24, 2007 post from the Open JavaFX Compiler Project "dev" mailing list. By the way, have I mention lately how amazed I am at the abilities and diligence of this team? :-D
Since block expressions allow you to place statements in an expression,
the overlap between if-then/if() and foreach/for has grown. First, I
should explain block expressions: they are loosely a sequence of
statements followed by an optional value, the value of the block
expression is that of the optional value.
{ var pi = 3.1415; pi * pi }
So the value of this block expression is pi squared.
They can be used anywhere an expression can be used (sorry for the
artificial example):
var x = if y > 0 then { var tmp = min(y, z); tmp * tmp } else { -y
};
If void values are allowed for an if-then, we can get all the
functionality of if() --
if y > 0 then { println("greater"); } else { println("lesser"); };
There seems little value in also having a statement-only form (even
confusing) :
if (y > 0) { println("greater"); } else { println("lesser"); };
Same if true of foreach/for:
var z = foreach (x in xs, y in ys where x < y) { x + y }
If foreach works over void values:
foreach (x in xs, y in ys where x < y) { println(x + y); }
Why would we need:
for (x in xs, y in ys where x < y) { println(x + y); }
?
-Robert
As you may have gleaned from Robert's post, because of block expressions, not only will the ternary operator and if-else be combined, but foreach and for will be combined into foreach. This is continued progress on achieving the "simplicity on the other side of complexity" -- one of the features that attracted me to JavaFX Script in the first place.
Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download available at the book's Apress site
I'm glad to see they are working on making the language better, sounds like Robert has some good points.
Posted by: codecraig | November 19, 2007 at 02:55 PM