Rob Wilson has posted an interested dive through some of the syntax oddities of JavaFX. Although I don't entirely agree with all of Rob's conclusions (JavaFX sure looks dynamically typed to me, very similarly to Groovy, and the fact it runs on a JVM underneath, and compiles to byte code, makes no odds to my knowledge) I found this article really interesting as a first glance at JavaFX as I've not had cause to look at it so far professionally or personally.
Also I'd like to answer one of Rob's open questions here. Rob asks "If this is true 'but functions will return null / void if no return type specified' then why does this return a number!... " for this code:
function test() {
return '9';
}
function test2() {
var x=10;
}
println("test = {test()}"); // returns 9 (as a String)
println("test2 = {test2()}"); // returns 10 (as a Number)
This makes sense to me. The JavaFX language spec mentions that variable declaration is an expression and so has a return value. That return value is the initial value of the dynamically typed variable, which in x's case is 10.
Ok, so the next bit is down to implicit return statements, which are a feature, that I'm not actually too fond of but, that is gaining traction through languages such as Ruby and Groovy. Simply put, the result of the final expression in the function is the result of the function, unless it is masked with a Void explicit return type (which states 'there is not return expected'). So test() returns the result of evaluating the declaration of the x variable, which as I mentioned before is 10. So the function returns 10, implicitly.
As I said, I'm not exactly keen on this type of implicit return, however it is becoming idiomatic of dynamically typed languages, so maybe I'll just have to get over that. For me it is as confusing and dangerous as when someone uses an if statement in Java without curly braces. I mean, c'mon, it's only a couple of braces ("it's only a return statement..."), but the danger of maintaining that code incorrectly (by accidentally adding another line of code to the if block, but not realizing there's no braces... or by changing the last statement of a function, without realizing that that completely changes what gets returned...) for me makes the implicit return a dubious best practice.
But then again, maybe my static-Java roots are showing, sorry about that!
Anyway, keep up the great work Rob, I'm looking forward to the next instalment.
