Converting to the New Syntax
From Planet JFX
The syntax for the JavaFX Script language is changing to further improve its clarity, ease-of-use, and support for compilation. As the language evolves, this page lists those changes and how to update your code.
Contents |
[edit] Operations
- Description: The language used to differentiate between functions and operations (procedures). The current syntax merges these two concepts into just one: function. Functions are no longer restricted to variable declarations and return expressions, and basically old-style operations with a new name.
- Conversion: Rename all "operation" keywords to "function".
class Foo {
function times2(x) { return x * 2; }
operation print(s) { System.out.println(s); }
}
New:
class Foo {
function times2(x) { return x * 2; }
function print(s) { System.out.println(s); }
}
[edit] Attribute Initialization
- Description: If a declared attribute had an initial value, it was set outside of the class body. Attribute initial values are now set as part of the declaration, like Java.
- Conversion:
class Foo {
attribute bar: Boolean;
}
attribute Foo.bar = true;
New:
class Foo {
attribute bar: Boolean = true;
}
[edit] Replace Triggers
- Description: Replace triggers were defined outside of the class body. Replace triggers are now part of the attribute declaration. Also, the syntax has changed, such that the trigger function now follows the keywords "on replace".
- Conversion:
class Foo {
attribute bar: Boolean;
}
trigger on Foo.bar = value {
if (bar == true) {
beep();
}
}
New: class Foo {
attribute bar: Boolean on replace {
if (bar == true) {
beep();
}
};
}
Old with Initialization: class Foo {
attribute bar: Boolean = true;
}
trigger on Foo.bar = value {
if (bar == true) {
beep();
}
}
New with Initialization:
class Foo {
attribute bar: Boolean = true on replace {
if (bar == true) {
beep();
}
};
}
[edit] Cardinality
- Description: In interpreted JavaFX the cardinality operator was represented by '*' asterisk. Now, in compiled JavaFX, we use square brackets.
- Conversion:
class Foo {
attribute names :String*;
}
attribute names = ["Monica", "Rachel", "Phoebe"];
New:
class Foo {
attribute names :String[] = ["Monica", "Rachel", "Phoebe"];
}
[edit] Object literals without attributes
- Description: In interpreted JavaFX, it was possible to mention just a class name to refer an object literal without attributes. In compiled JavaFX, curly brackets are required.
- Conversion:
Frame {
title: "Show MenuSeparator"
height: 180
width: 320
menubar: MenuBar {
menus: Menu {
text: "File"
items: [MenuItem {
text: "New"
}, MenuItem {
text: "Open"
}, MenuItem {
text: "Save"
}, MenuSeparator, MenuItem {
text: "Import"
}]
}
}
visible: true
}
New:
Frame {
title: "Show MenuSeparator"
height: 180
width: 320
menubar: MenuBar {
menus: Menu {
text: "File"
items: [MenuItem {
text: "New"
}, MenuItem {
text: "Open"
}, MenuItem {
text: "Save"
}, MenuSeparator {
}, MenuItem {
text: "Import"
}]
}
}
visible: true
}
[edit] Named instances
- Description: In interpreted JavaFX, it was possible to use named instances that more or less represented what is now required in compiled JavaFX - the fully qualified constants, or to use object literals.
- Conversion:
Frame {
title: "White Frame"
background: white
...
}
New:
Frame {
title: "White Frame"
background: Color.WHITE
...
}
Or:
Frame {
title: "White Frame"
background: Color {
red: 1
green: 1
blue: 1
opacity: 1
}
...
}
[edit] Anonymous object literals
- Description: In intepreted JavaFX, we could use anonymous object literals and the intepreter inferred type of such block of code. In compiled JavaFX, we need to use named object literals.
- Conversion:
...
accelerator: {
modifier: CTRL
keyStroke: O
}
....
New:
...
accelerator: Accelerator {
modifier: KeyModifier.CTRL
keyStroke: KeyStroke.O
}
...
[edit] Function overriding
- Description: When you override a function, it's needed to mention its return type in function's signature.
- Conversion:
class MyWidget extends CompositeNode {
...
function composeNode() {
...
}
}
New:
class MyWidget extends CompositeNode {
...
function composeNode() :Node {
...
}
}
[edit] Bidirectional binding
- Description: If you want to use the bidirectional binding feature, you have to use new 'with inverse' syntax in compiled JavaFX.
- Conversion:
...
TextField {
value: bind model.firstName
}
...
New:
...
TextField {
value: bind model.firstName with inverse
}
...
[edit] Casting from Number to Integer
- Description: To avoid loss of precision error during compilation, use the intValue() function.
- Conversion:
... var real :Number; num = 6.42; var integer :Integer; integer = real; ...New:
... var real :Number; num = 6.42; var integer :Integer = real.intValue(); ...
[edit] Inheritance
- Description: In current state of JavaFX compiler (as of March 2008) it may be needed to use 'as' construct as a workaround to avoid inheritance issues.
- Conversion:
class Foo extends Rectangle {
...
}
...
...
content: Canvas {
content: Foo {
...
}
...
}
...
}
New:
class Foo extends Rectangle {
...
}
var foo :Foo = Foo {
...
};
...
content: Canvas {
content: foo as Node
...
}
...
}
[edit] Loops
- Description: The foreach loop was deprecated and the only one foreach-like loop in compiled JavaFX is for loop. You can use the for loop as you're used to use from Java fro example and also as a foreach loop from interpreted JavaFX.
- Conversion:
...
for (Integer i = 0; i < element.length; i++) {
System.out.println(element);
}
...
foreach (element in group) {
System.out.println(element);
}
...
New:
...
for (Integer i = 0; i < element.length; i++) {
System.out.println({element});
}
...
for (element in group where element.length() < 4) {
System.out.println({element});
}
...
