Actually, it's both. As I mentioned in the Express Yourself post, compiled JavaFX Script has combined the functionality of for and foreach into just the foreach keyword. I also explained in that post that compiled JavaFX Script has combined the if/else statement and ternary expression into just the if/else syntax. Here's an example that you can compile and run to demonstrate this for yourself:
/*
* CompiledIfElseExample.fx - A simple example in compiled JavaFX Script of
* defining and iterating over a sequence.
*
* Developed 2007 by James L. Weaver (jim.weaver at lat-inc.com)
*/
import java.lang.System;
class CompiledIfElseExample {
function getAlias(name:String):String {
var aka:String;
if (name == "JAWA THE HUT" or name == "JAVA THE HUT") {
aka = "The Garbage Collector";
}
else {
aka =
if (name == "JAH-VA") "The Hashcode Specialist"
else "Captain of the Mustang"
}
return aka;
}
}
var example =
CompiledIfElseExample {
};
System.out.println("JAWA THE HUT: {example.getAlias("JAWA THE HUT")}");
System.out.println("JAH-VA: {example.getAlias("JAH-VA")}");
System.out.println("DUKE: {example.getAlias("DUKE")}");
What's all this Jawa the Hut Stuff?
You caught me. An ulterior motive for writing these blog posts nearly every day for the last month or so is to be prepared for speaking at the upcoming JavaPolis 2007 conference in Antwerp, Belgium. I'll be doing two sessions on JavaFX: A three-hour University session on December 11, 2007, and a one-hour conference presentation on December 12, 2007. The theme for the JavaPolis conference this year is Star Wars meets JavaPolis, so I crafted today's example to match the theme. If you are in attendance, please introduce yourself! By the way, these blog posts are not just to prepare for the JavaPolis conference -- some may appear in my next JavaFX book ;-) Seriously though, I plan on posting nearly daily to this blog until JavaFX is the de facto standard for creating rich internet applications.
Back to the Example
This example shows the if/else construct as a statement beginning with the following line:
if (name == "JAWA THE HUT" or name == "JAVA THE HUT") {
This form of if/else is identical to Java. Like Java, the curly braces are optional. What isn't like Java in this line is string equality. Notice that it is legal in JavaFX to compare strings with the equality operator (==), but not in Java, where the equals() method is typically used for string equality. Also different from Java is the or operator, which corresponds to the short-circuit or operator in Java (||).
This example also shows the if/else construct as an expression that returns a value in the following lines:
aka =
if (name == "JAH-VA") "The Hashcode Specialist"
else "Captain of the Mustang"
In this case, the if/else construct replaces the ternary operator (?:) in Java, which evaluates to a value based upon the outcome of the if test.
Enjoy playing with this example, and as a reminder, the next JavaFX Puzzler will be be posted at 18:00 GMT (1:00 pm EST) on Wednesday, November 28, 2007.
Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site
Very funny Java(FX)Polis theme example :)
Posted by: Stephan | November 28, 2007 at 04:40 AM