SnappRadio FX Source Code

Thursday, October 23rd, 2008

Here’s the JavaFX source code for SnappRadio FX. JavaFX code is pretty readable, so you shouldn’t have much trouble understanding what’s going on even if you’re not familar with the syntax.  Main.fx is a good place to start if you want to trace through. As always, with all of my FX code, it likely does not conform to Best Practices, and since it was a learning exercise, any number of conflicting ideas are embedded within. Having said all that, here’s a quick highlight of some language features that someone with a Java background might do a double take on.

Expressions

JavaFX is an expression based language, meaning that every expression returns a value. So we have the obvious:

var x = if (condition) 0 else 1

which obviates the need for a special ternary operator. But then we also have the less obvious:

var nums = for (i in [0..10]) {i}

Keyframe Interpolation

Here’s a KeyFrame:

KeyFrame {
  time: 1s
  values: [curX => inspectX tween Interpolator.EASEBOTH,
  curY => 300 tween Interpolator.EASEBOTH,
  scale => 0.7 tween Interpolator.EASEBOTH]
}

Interpolation is aided with the help of the adorable “tween” keyword. “tween” is only slightly less adorable than JavaFX’s spaghetti operator: pssghetti. (groan)

Type Inference

JavaFX is capable of some amount of type inference.  So you can do this:

attribute dismissed = false

but if it’s ambigious you have the strict typing option:

attribute startX:Number = 0

I hope you like SQL

JavaFX shares a lot of language constructs that are reminiscent of SQL, such as:

delete n from nodes

and:

insert n into nodes

and triggers like:

attribute curY:Number = -500 on replace oldVal = newVal {updateReflection()}

Don’t forget your semis!  Or forget them…or whatever…

I’m still not clear under which contexts semi-colons are required to terminate a statement.  They seem to be optional in some places, and required in others.

Predicates

A nice language feature is the predicate syntax:

 var dismissed = nodes[n| n.dismissed]

Which in this case returns a new sequence of Nodes from “nodes” that are of the state “dimissed”.

Anyway, that’s all for now…

Tags:

Leave a Reply