[xquery-talk] function declaration vs expression

Ghislain Fourny g at 28.io
Mon Mar 14 06:18:24 PST 2016


Hi WS,

Higher-order functions were added in XQuery 3.0 so that you can build
them dynamically (using closures) and pass them as parameters like any
other items.

I see little benefit in defining your function as a variable
declaration with an inline function expression vs. a static function
declaration such as

declare function local:id($x) {
  $x
};

You can reference the former with $local:id and the latter with
local:id#1 to make them flow around as function items. You can call
the former with $local:id($x) and the latter with local:id($x). The
former has no name, the latter has a name (local:id).

When you need a function that dynamically depends on the value of a
variable at runtime, however, you will need to use inline function
expressions. Variables in scope will be added to the closure of the
created function item.

I hope it helps!

Kind regards,
Ghislain

On Mon, Mar 14, 2016 at 2:48 PM, W.S. Hager <wshager at gmail.com> wrote:
> Hello,
>
> What is the difference between a function declaration and a function
> expression in a variable declaration, eg:
>
> declare variable $local:id := function($x) { $x }
>
> and why does it exist?
>
> --
>
> W.S. Hager
> Lagua Web Solutions
> http://lagua.nl
>
>
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk


More information about the talk mailing list