Coercion Rules
In 4.0, the term coercion rules replaces function conversion rules.
These rules determine how the supplied value for a function argument or XSLT variable can differ from the declared type.
In XQuery 4.0, the coercion rules are now used to convert variable values to their required
type. This includes both global variable declarations and local variables bound in let
,
for [member]
, and group by
clauses of a FLWOR expression.
Automatic downcasting
The coercion rules now allow downcasting. For example, if the required type is declared as xs:positiveInteger
then the supplied value can be 42
: there is no longer any need for the caller to cast it as
xs:positiveInteger(42)
. Of course, an error occurs if the cast fails. The effect of this is to make
derived types such as xs:positiveInteger
much more usable. Note that this rule doesn't allow any
casting operation, only a strict relabelling of the supplied value. For example, if the required type is xs:integer
then you can supply the decimal value 3.0, but you can't supply 3.1, even though casting 3.1 to xs:integer
is allowed.
Arity reduction
The function coercion rules are extended to allow a supplied function item to have lower arity
than that implied by the signature of the required type. For example, map:for-each()
expects a function with two
arguments, which are set respectively to the key and the value of an entry in the map. But if you are only interested in the key,
you can supply a function of arity 1, and your function will be called omitting the second argument. Similarly,
for a function that expects a predicate (a function of arity one), you can now supply the value fn:true#0
which has arity zero: this has the effect that the predicate will always be true.
This feature is implemented from Saxon 12.3.