Tuesday, February 9, 2010

JavaFx: Interesting Language Features

Today's post takes a look at a few of the JavaFx language features that I find interesting. It's by no means an exhaustive rundown of the language; for that, take a look at the language reference (I expect this link to change at some point in the future — right now it points to a build system). And the most interesting feature, binding, will be the topic of its own post.

This is going to be a long post, so get a cup of coffee.

Scripts

A JavaFx source file is called a “script,” and looks a lot like a JavaScript source file: unstructured, with variable definitions, functions, and inline executable code all jumbled together. But, as I noted earlier, this “script” is actually compiled into a Java class.

This class definition has a lot of boilerplate in it to bridge the gap between features of JavaFx and JVM bytecode. All of this boilerplate is, of course, implementation dependent and subject to change. One piece is the static method javafx$run$(), which serves the same function as Java's main(); the javafx program looks for this method when it starts a JavaFx application.

Another implementation detail, and one that I think is a little disturbing, is that all of the script's variables and functions are represented as static class members. I suspect that this detail is driven by the idea that scripts a primarily used for initialization of the GUI, and most action takes place in classes.

Class Members and Initialization

Unlike Java, but like JavaScript, classes are happy to expose their internals: the language includes access modifiers, and the language reference indicates that the default is script-level access, but the generated code says that everything is public.

Rather than providing constructors, you initialize instances using an “object literal” notation that's very similar to JavaScript's object literals:

class MyClass {
    var a : Integer;
    var b : String;
}

var z1 = MyClass { a: 123, b: "456" };

There's no new! And I should mention that JavaFx is very liberal with regards to delimiters in an object initializer: here I separated my initializers with a comma, which follows the JavaScript practice. However, I could have used a semi-colon, or simply omitted the delimiter altogether; the JavaFx compiler would recognize where each item ended. I still haven't figured out all of the rules, so simply follow Java practice: semi-colons at the ends of statements, and commas between items in a list.

Mixin Inheritance

The Java inheritance rules are imposed by the JVM: a given class may have only one superclass, but may implement any number of interfaces. JavaFx appears to bend the rules : you can create “mixin” classes, similar to Scala traits, and a JavaFx class can extend more than one mixin:

mixin class Foo {
    var x : Integer;
}

mixin class Bar {
    var y : String;
}

class Baz
extends Foo, Bar {
    override function toString() : String {
        "[{x},{y}]";
    }
}

var a = Baz { x : 123, y : "456" };
println(a);

Of course, JavaFx doesn't break the rules: the JavaFx compiler translates mixins into composition. The Baz class actually holds instances of Foo and Bar, and has getter and setter methods that delegate to those instances. Incidentally, you won't find mixins in the current language reference; you need to go to the tutorials.

This example shows one additional feature of inheritance: you have to explicitly override member variables and methods. Here we implement a custom toString() method. Since that method is already defined by java.lang.Object, which is the ultimate ancestor of any JavaFx class, we need to mark it as explicitly overridden. As far as I can tell, this is simply a means of error-checking, similar to Java's @Override annotation; JavaFx does not impose “final unless defined otherwise” semantics.

Variables, Definitions, and Type Inferencing

In the class examples above, you saw the canonical variable declarations:

var NAME : TYPE;

At first glance, this appears to be a case of the language designers wanting to look like ActionScript and not Java; it's semantically equivalent to a Java variable declaration, which puts the type first. However, JavaFx also supports type inferencing: you can declare and initialize a variable at the same time, and the compiler will figure out what type you're using:

var a = "foo";
var b = a;

For people tired of constantly repeating long variable declarations in Java, type inferencing is a godsend. For people used to JavaScript, which doesn't have strong typing, they may find type inference annoying:

var a = "foo";
a = 123;        // this won't compile

To close out the discussion of variables, I'll note that JavaFx also provides the def keyword, which is similar to Java's final modifier:

def x = 123;
x = 456;        // won't compile

I'm dubious regarding the value of def. With multi-threaded programming, it's very useful to declare that a “variable” is actually immutable. Scala, for example provides the val keyword to do this. However, the JavaFx def keyword does not imply immutability: it can reference a bound expression, which may change during runtime. Until the language matures, and develops truly different semantics for def versus var (and I'm not sure what those might be), I plan to stick with var for all variables.

Blocks Have a Value

Most “curly brace” languages use those braces to make a group of statements syntactically equivalent to a single statement. For example, in Java:

for (int ii = 0 ; ii < 10 ; ii++)
    System.out.println("this is in the loop");
System.out.println("this isn't");


for (int ii = 0 ; ii < 10 ; ii++) {
    int zz = 12 * ii;
    System.out.println("this is in the loop");
    System.out.println("as is this");
}
System.out.println("this isn't");

Blocks also introduce a new scope for variables: in the example above, the variable zz is only accessible from code within the block. In JavaFx, blocks have an additional feature: the last expression in the block becomes the value of that block:

var z = {
    var x = 10;
    var y = 20;
    x + y;
};

Interesting, but what's the point? Well, consider that in JavaFx the the if keyword introduces an expression, not a statement. So you get the following formulation of a ternary expression:

var x = 12;
var y = if (x < 20) {
            var z = x / 2;
            z - 17;
        }
        else {
            23;
        }

And not only is if an expression, but for is as well. Which is particularly useful when generating sequences (the JavaFx equivalent of an array):

var seq = for (x in [1..5]) {
              [x, x*x, x*x*x];
          }

Explaining that last example would be a blog posting on its own. I suggest however, that you try it out if you've downloaded a development environment.

Closures

Closures are a hot topic in modern programming languages. As C programmers have known for decades, function pointers are an incredibly powerful tool (Lisp programmers have known this for longer, but such knowledge is indistinguishable from their general smugness). A big benefit of function pointers is that they let you abstract away a lot of boilerplate code, particularly in the area of resource management. For example, here's a typical piece of Java database code:

        Connection cxt = DriverManager.getConnection("url");
        PreparedStatement stmt = null;
        try
        {
             stmt = cxt.prepareStatement("sql");
             ResultSet rslt = stmt.executeQuery();
             // process result set
        }
        catch (SQLException ex)
        {
            // do something with the exception
        }
        finally
        {
            if (stmt != null)
                stmt.close();
            if (cxt != null)
                cxt.close();
        }

That boilerplate try/catch/finally must be repeated for every single SQL statement that you execute. And as written, it has a bug: the call to stmt.close() can throw, in which case the Connection will never be closed. If you cache PreparedStatements, the code becomes even more complex. With first-class functions, you can implement the boilerplate in a DatabaseManager class, and define a function that just processes the result set:

var dbManager = DatabaseManager { /* config */ };
dbManager.executeQuery(
            "sql",
            function(rslt:ResultSet) {
                // process results here
            });

In this example, we create a new function object as an argument to the executeQuery() method. This method can then call the function, passing in the ResultSet that it gets after executing the query. Once the method finishes, the function will be garbage-collected.

The example above is a bit abstract, so here's a simple compilable script:

var fn = function(x, y) {
    x + y;
}

var z = fn(12, 13);
println(z);

Here we define a variable, fn, that holds a function. We then execute the function, storing the result in variable z. We could have passed the function call directly into println(), and we could pass the function itself to some other function.

But wait, there's more! JavaFx implements true closures, where a function has access to the variables defined in its enclosing scope:

var a = 17;
var b = 23;
var c = function()
        {
            var t = a;
            a = b;
            b = t;
        };

println("before swap: a={a}, b={b}");
c();
println("after swap: a={a}, b={b}");

Personally, I don't particularly like closures. You could invoke c from any point in the code, perhaps in a completely different script; this is a great way to make your program behave in an indeterminate and hard-to-debug manner. In cases where you actually want the ability to update script variables from somewhere else, binding is probably a better option; I'll write about that tomorrow.

To close out this post, I want to note that JavaFx does not support lazy evaluation of functions. To my mind (if no-one else's), that's a big part of what makes a “functional” programming language: you can pass a first-class function to another function that expects, say, an integer, and the passed function will be evaluated when needed to provide the value.

Monday, February 8, 2010

Getting Started with JavaFx

As my last posting noted, I'm intrigued by the JavaFx language. It's being marketed as a platform for Rich Internet Applications (RIAs), in competition with Flash (Flex) and Silverlight. I don't think it's going to be successful in that space, for reasons that I'll detail in a future post. However, it might just be the next big language for the JVM platform, a functional-object language with the marketing muscle of Oracle behind it.

With that in mind, I'm taking some time to investigate the language, and will be writing a series of posts about it. I won't spend a lot of time on the GUI aspects; there are already blogs that do that, and a whole page of examples with source code. Instead, I want to point out some of the things that I think are neat about the language, and some of the roadblocks that might be in its way as a general-purpose environment.

There will be a few code snippets along the way. You can compile and run them from the command line, using the javafxc and javafx tools from the SDK. There's also IDE support for NetBeans 6.x and Eclipse. I use the NetBeans plugin, and it seems fairly solid (although there seem to be some issues with NetBeans on Ubuntu 9.04 — sometimes it stops accepting keystrokes until I switch windows). If you don't already have NetBeans, you can download it pre-configured with JavaFx, from the JavaFx downloads page (and you'll find the SDK there too).

Before diving into code, there are a couple of terms that have special meaning with JavaFx:

script
A JavaFx source file. Although called a “script,” these files must be compiled into Java bytecode, and run from within the JavaFx environment. Scripts are translated into top-level Java classes with predefined methods used to invoke the script (think of main()), along with the various methods, variables, and code defined by the script.
class
Much like a class in any other language: a definition that combines data and methods, and must be instantiated for use. JavaFx classes are defined within a script, and the compiler creates nested Java classes to represent them.

Now it's time for “Hello, World”:

package experiments.language;

println("Hello, World");

That's it. It really does look like a script: there's no boilerplate class definition, no public static void main definition. Even the println() statement gives no hint that it's actually implemented by the class javafx.lang.Builtins.

All of this, however, is simply syntactic sugar: you could easily write a precompiler that takes a similar script and adds the boilerplate Java code. I'll leave you with something new, however: inline variable substitution, a la PHP:

package experiments.language;

def addressee = "World";
println("Hello, {addressee}");

Thursday, February 4, 2010

Book Review: Essential JavaFx

I recently received a copy of Essential JavaFx as book give-away at my local Java Users Group meeting. The JUG receives such books directly from publishers, with the expectation that JUG members will write reviews. My review of this book follows; I suspect that Dave won't be sending it back to the publisher.


My bookshelf holds a dog-eared copy of The UNIX C Shell Field Guide, written by Gail and Paul Anderson in 1986. To me, it represents the perfect mix of tutorial and reference, presenting an enormous amount of technical detail in a careful progression. Unfortunately, I can't say the same for their book Essential JavaFx: the material is disjointed, skimming the surface of many topics in seemingly random order, without truly examining any of them.

This book begins, as nearly all programming books do, with a simple “Hello, World” example. This part is, in fact, a little too simplistic: it walks the reader through specific screens and buttons of the NetBeans IDE. The actual program appears only as a screen-shot, without explanation of its structure. For a book that is targeted to an audience with “some previous programming experience,” this is disappointing: I want to see the program, not the IDE.

Chapter 2 attempts to remedy that initial surface treatment, by presenting a relatively complex example application. And it starts well, with a description of the way that a JavaFx application is built around a “scene graph,” and the admonishment to “think like a designer [...] visualize the structure of your application.” But then, it dives into variable definitions and control constructs, without so much as mentioning how they fit into the scene graph. And from there, the same chapter touches on accessing Java classes, visual effects, lazy expression evaluation (not identified as such), and animations &mash; a few paragraphs or pages for each, without explanation as to how they might fit together in an application.

Chapter 3 is a whirlwind treatment of the core JavaFx language. And there is a lot to this language: it is far closer to Scala than to Java, with influences from Python. Indeed, I could envision an entire book exploring the effective use of these core language features, perhaps even ignoring the GUI.

The subsequent chapters are much the same: quick hits without real content. Chapter 6, “Anatomy of a JavaFx Application,” is the closest that we get to a step-by-step tutorial, yet even here the Andersons forget their early advice: rather than starting with the application structure, they immediately create component subclasses, and only later assemble them into a scene graph.

In the end, I put the book down and turned to my IDE and “experiential learning.”


So, not so great a review of the book. But the language looks fascinating, and I plan to spend time working with it over the next few weeks. My first impression is that it might in fact be “the next big language” for the JVM: it seems to be a nice combination of strong typing, declarative configuration, and functional/object execution. I'm wondering if, like Java before it, JavaFx might be introduced to the world as a GUI language, yet end up as a language for server-centric applications.