Saxon-EE implements the new facilities for higher-order functions in XQuery.
An XPath item may be a function item, that is, a dynamic reference to a function. Function items can be manipulated in the same way as any other item, for example they can be held in variables, passed as parameters, returned as a function result, or arranged in sequences of items. In Saxon function items are currently represented as primitive atomic values, unlike the Working Draft which represents them as a third kind of item alongside atomic values and nodes.
An expression such as fn:contains#2
returns a function item bound to the 2-argument version
of the fn:contains
function. Saxon previously allowed this using the syntax saxon:function('fn:contains', 2)
.
Such an expression will often appear as an argument to a higher order function that expects a function item
as one of its arguments.
If the value of a variable $f
is a function item, the function can be invoked using the
syntax $f(x, y, z)
. Here $f
can be any primary expression or filter expression so long as its
value is a single function item. Saxon previously allowed this using the syntax saxon:call($f, x, y, z)
.
The type of a variable or function parameter can be declared as function()
to indicate that
the expected value is a function item. It is also possible to declare the full signature, for example
function(xs:int, node()*)
as xs:string?.
Inline (anonymous) functions can be written, for example function ($x as xs:integer) as xs:boolean {$x mod 2 eq 0}
.
Such a function will typically be used as an argument in a function call expecting a parameter of type function()
.
The functions fn:function-name()
, fn:function-arity()
, and fn:partial-apply()
are implemented.
Saxon applies function coercion when a function is passed to another function, or when it is returned as a function result.
However it also implements a proposed change to the specification whereby function coercion is not used for operations such
as "instance of".
These follow stricter type checking rules: a function F(A,B)->T
is an instance of a type F(X,Y)->U
if every T is
an instance of U, every X is an instance of A, and every Y is an instance of B.