One of the types supported by JavaFX is named a function type. To be clear, this is not the type of the returned value of the function, but rather an actual data type that represents a function. This versatility throws JavaFX squarely in the realm of functional programming, where functions are regarded as first-order data types and can be manipulated just like any other supported data types. This section shows you how to create functions in JavaFX and use them as expressions in your code.
The concepts presented here discuss functions as an executable code unit that can be assigned and reused. You are expected to know the general purpose of a function and how to use it. If you have written any code before, you most likely know how to create and use a function.
In JavaFX, A function is a specialized code block preceded by the function
keyword. It can accept zero or more typed parameters and always returns a typed value. Here is the declaration of a function type assigned to variable called squareIt
, which returns the squared value of the number passed in as parameter. Complete code listing can be found at ch01/source-code/src/javafx/SimpleFunction.fx
.
var squareIt : function(:Number):Number; squareIt = function (x) { x * x; } var square3 = squareIt(3); println ("3 squared = {square3}");
In JavaFX, a function has a distinct, definable type (similar to String, Number, and Integer). A function type is defined by its parameter signature and its return type. Variables (and parameters) can be assigned a function type. For instance, in the previous snippet, variable squareIt
is declared as being a function type. This means that the variable squareIt
can be assigned a function that takes one parameter of type Number
and returns a value of type Number. squareIt
can be used anywhere a function call can be used, as shown in the call var square3 = squareIt(3)
.
Note that the declaration and definition of the function can be combined in one step, as show next:
function squareIt(x:Number):Number{ x * x; }
The JavaFX compiler can infer the types associated with a function's parameter signature and return value. Therefore, the function definition can be reduced to:
function squareIt(x) { x*x; }
The type inference engine in JavaFX will determine the proper type of the parameter based on value of the parameter at runtime. The return type of the function is based on the type of the last statement in the function or the type of the value used in the return
statement.
There are couple more features about functions in which you may be interested.
Since a function is a considered to be an expression in JavaFX, it can be bound to a variable (similar to a code block binding, see Using Binding and Triggers to Update Variables).
var f = 10; bound function increaseIt(a:Number):Number { a + f; } var x = 5; def y = bind increaseIt(x);
When a function is defined as being bound, any change to values inside the function block (including its parameters) will cause an update to the binding variable. Here, whenever variable f
or x
changes, the value of y
is updated automatically.
JavaFX offers a way to define a script file's main entry point using the special script-level function run()
. If you place the following in a script file:
function run() { println ("I am always called!"); }
When you execute the script, the run()
function will be executed as the starting point of the script by the JavaFX runtime. This similar to having the public static void main(String[] args)
method in Java.
When you create a script file with script-level code without run()
, the compiler creates one for you and places your script's code inside of it. As such, your script seems to execute top to bottom. However, when you provide your own run()
, that is no longer the case. The JavaFX runtime will only call whatever code is inside of the run()
function.