Question / Suggestion : Template mechanisms and operator overloading for XQUERY 3.++ ?
Sunday, 4 January 2015 · 2 messages · last reply 6 Jan 2015
JM
jean-marc Mercier4 Jan 2015 11:05 #
Hello,
I was wondering if it is possible to improve template programming and operator overloading with XQUERY 3.?
1) template programming : XQUERY provides a quite natural template mechanism using type switch and instance:
declare function yell($animal) {
if ($animal instance of element()) thenreturn typeswitch($animal)
case element(cow) return "Moo" default return "..."
else "this is not an animal"
};However, I wonder whether it could be possible 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, XQUERY functions identification seems to be achieve through a couple (name, arity). Would it be difficult to extend it with (name,signature or predicate) ?
2) Operator overloading (quite linked to the previous question)
I don't know how to overload operators with XQUERY. For instance I would like to define something like
declare function array:operator+($left,$right){array:for
-each-pair($left,$right,function($a + $b) { $a + $b } )}Is it possible ?
I know that I can survive writing
declare function array:plus($left,$right){array: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)
CG
Christian Grün6 Jan 2015 12:23 #
Hi Jean-Marc,
It may be worthwhile to discuss this general XQuery feature request on talk at x-query.com or the W3 bug tracker [1].
Best,
Christian
[1] https://www.w3.org/Bugs/Public/
Quoted text · jean-marc Mercier · 55 lines
On Sun, Jan 4, 2015 at 11:05 AM, jean-marc Mercier <jeanmarc.mercier at gmail.com> wrote: > Hello, > > I was wondering if it is possible to improve template programming and > operator overloading with XQUERY 3.? > > 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" > }; > > However, I wonder whether it could be possible 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, XQUERY functions identification seems to be achieve > through a couple (name, arity). Would it be difficult to extend it with > (name,signature or predicate) ? > > 2) Operator overloading (quite linked to the previous question) > > I don't know how to overload operators with XQUERY. For instance I would > like to define something like > > declare function > array:operator+($left,$right){array:for-each-pair($left,$right,function($a + > $b) { $a + $b } )} > > Is it possible ? > > I know that I can survive writing > > declare function > array:plus($left,$right){array: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) > > > >
Written for BaseX 7.9. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery