[xquery-talk] Question / Suggestion : Template mechanisms and operator overloading for XQUERY 3.++ ?

jean-marc Mercier jeanmarc.mercier at gmail.com
Thu Jan 8 23:51:19 PST 2015


Hello,

I would like to discuss the opportunity to improve template programming and
operator overloading for XQUERY ?

1) template programming : XQUERY provides a quite natural template
mechanism using type switch and instance:

declare function yell($animal) {
if ($animal instance of element()) then

return typeswitch($animal)
  case element(cow) return "Moo"  default return "..."

else "this is not an animal"
};

I think that it could be worthwhile to provide such a mechanism directly at
the interpreter level. For instance, the following code seems coherent and
concise :

declare function yell($animal as element(cow) ) {"Moo"}
declare function yell($animal as element() ) {".."}
declare function yell($animal ) {"This is not an animal"}

As far as I understand, the previous code violates  XQUERY functions
identification, achieved through a couple (name, arity). Would it be
difficult to extend this identification mechanism ?

2) A quite interesting application to 1) would be to define operator
overloading

Indeed, I don't know how to overload operators with XQUERY. For instance I
would like to define something like

declare function operator+($left,$right){fn:for-each-pair($left,$right,
function($a + $b) { $a + $b } )}

I know that I can survive writing

declare function local:plus($left,$right){fn:for-each-pair($left,$right,
function($a + $b) { $a + $b } )}

but when one start to write algorithmic code, it is far easier and
understandable to write $a+$b than array:plus($a,$b). Moreover, it can be
used to expressions template, quite useful for linear algebra.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://x-query.com/pipermail/talk/attachments/20150109/e55a9628/attachment.html>


More information about the talk mailing list