Dynamic function invocation

Wednesday, 2 November 2011 · 2 messages
AB
Andy Bunce2 Nov 2011 17:07 #
declare function local:add($x){
$x+1
};
let $b:=local:add
return $b(4)
Produces [XPTY0004] Variable(...): no empty sequence allowed. Have I got the syntax wrong, or is this a bug? /Andy
LW
Leo Wörteler2 Nov 2011 17:28 #
Hi Andy,
Quoted text · Andy Bunce · 8 lines
Am 02.11.2011 17:07, schrieb Andy Bunce: > declare function local:add($x){ > $x+1 > }; > let $b:=local:add > return $b(4) > [...] > Have I got the syntax wrong, or is this a bug?
it's the syntax, the expression "local:add" is interpreted as an axis step. Function item literals have to specify the number of arguments, as there may be different functions with the same name. So you'd have to write local:add#1 .
You could also use partial application (local:add(?)) or write your own
inline function wrapping the function call (function($x) { local:add($x)
}). All three versions compile to basically the same code:

declare function local:add($x){ $x+1 };

let $a := local:add#1,
     $b := local:add(?),
     $c := function($x) { local:add($x) }
return ($a(1), $b(2), $c(3))
Hope that helps! Cheers, Leo

⚡Generated with XQuery