Rob Wilson on JavaFX language

Posted by: Russell Miles on 2009-09-08 03:29:20.0

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.


be the first to rate this blog

About Russell Miles

Russell Miles

A self-confessed polyglot programmer, Russ Miles is a senior consultant for SpringSource in the UK where he works with various companies to help them take full advantage of the Spring Framework. To ensure that he has as little spare time as possible, Russ contributes to various open source projects and has authored a number of books.

Russ is a keen contributor to open source projects and an author for O'Reilly Media. He has authored and co-authored 3 books; "AspectJ Cookbook", "Learning UML 2.0" and "Head First Software Development"

Prior to joining SpringSource, Russ gained experience of enterprise development throughout all tiers of application architecture including high performance and usability presentation tier services for the Search and Mobile Portal industries right through to maximum availability application and data services for the Defence industry.

Russ holds an MSc. Software Engineering from Oxford University.

More About Russell »

NFJS, the Magazine

2009-12-01 00:00:00.0 Issue Now Available
  • Hibernate Performance Tuning, Part 2
    by Scott Leberknight
  • Virtualization for Development
    by Pratik Patel
  • Emergent Design & Evolutionary Architecture
    by Neal Ford
  • Writing Secure Code with ESAPI
    by Ken Sipe
Learn More »