JavaFX

Whaddayaknow, Sun has created a new “family of products”, JavaFX. This thing promises to “simplify and speed the creation and deployment of high-impact content for a wide range of devices”. The benefits are that it “reduces integration costs, improves device software consistency, and enables handset manufactures to provide new offerings with substantially faster time-to-market”. You have to hand it to Sun’s marketing people that’s top grade bullshit.

But if you look beyond the marketing crap one member of this “family of products” is a brand new programming language, JavaFX Script. There hasn’t been written a lot about it yet but I did manage to find this article that gives a quick introduction.

It is not a general-purpose language but:

[…] designed to optimize the creative process of building rich and compelling UIs leveraging Java Swing, Java 2D and Java 3D for developers and content authors.

Behind the superficial similarities with Java and JavaScript this new language has some very interesting features of its own. Here are a few of the features that caught my interest.

It is an object oriented language (runs on the JVM) but not everything is an object.

Arrays represent sequences of objects. In JavaFX arrays are not themselves objects, however, and do not nest. Expressions that produce nested arrays […] are automaticallly flattened […]

Hmm…

Arrays have a special status in the JavaFX language and are supported by some special syntactic constructs, for instance

var ints = [1, 2, 3, 4, 5];
insert 10 after ints[. == 3]
// ints is now [1, 2, 3, 10, 4, 5]

Note that the language not dynamically typed as the var declaration might seem to suggests; the ints variable is statically inferred to be an array of integers. Also note the use of a predicate to identify an element of an array. This is pretty neat:

var smallInts = ints[. < 4];
// smallInts is now [1, 2, 3]

JavaFX distinguishes between procedures and pure functions. There is a special syntax for declaring a pure function:

function dist(x, y) {
var xSqr = x * x;
var ySqr = y * y;
return sqrt(xSqr + ySqr);
}

A pure function declaration must be a sequence of variable declarations and a single final return statement. I guess they check statically that all functions called from within a pure function are also pure. If a procedure has side effects it must be declared with an operation declaration.

There are list comprehensions (array comprehensions?) with a relatively standard syntax:

select n * n from n in [1 .. 100]

Is is really so hard to find a list comprehension syntax where the declaration of the variable doesn’t come after its use? Apparently…

While the syntax is Java-like there are some syntactic differences from other languages in the C family. For logical operators they use the keywords and, or and not rather than &&, || and !. They also don’t mandate parentheses around conditions in if and while statements. Those are great changes, especially the logical operator keywords; this,

not (a == null or a.isEmpty())

reads much better than

!(a == null || a.isEmpty())

Also, if the precedence is right, it means that you can write

if (not x instanceof Foo) ...

rather than

if (!(x instanceof Foo)) ...

Another great feature is string interpolation; you can write stuff like

var answer = true;
var str = "The answer is {if answer then "Yes" else "No"}.";
// str is now "The answer is Yes."

I’ve used string interpolation in various languages and it’s great to have.

There is a reflective interface that looks a lot like Java’s except that it looks more straightforward and easier to use. There are a few extensions; a particularly spicy one is an operation that yields the extent of a class. That’s right, the syntax *:Foo yields an array of all instances of class Foo:

// Print all strings in this program
for (str in *:String)
System.out.println(str);

The clever reader might wonder if the result of *:Array contains itself since the result is an array? The really clever reader remembers that arrays aren’t objects and so can’t be enumerated by *:.

Why any language should have such a feature is a mystery to me and, it seems, also to them:

Note: this is an optional feature and is disabled by default.

Hopefully they will come to their senses and remove it completely before it’s too late and they have to maintain an implementation.

There are many more interesting features in this language which you can read about in the language reference. An implementation can be downloaded from their website; it runs on the JVM.

Overall it looks like a language with its very own peculiar flavor not quite like any languages I know. It’ll be interesting to see how well it succeeds in attracting programmers.

3 Responses to JavaFX

Leave a Reply

Your email address will not be published. Required fields are marked *


*