[xquery-talk] best practice for function design with many optional params

Jakob Fix jakob.fix at gmail.com
Thu Jan 26 00:13:47 PST 2012


I'm confronted with the design of a function library where many
functions have loads of optional arguments.

I understand in XQuery I can create any number of functions with the
same name as long as the signature is differentiated by the number of
arguments. This is cumbersome if I have many arguments, and also if
there is no precedence (i.e. the third optional param could appear on
its own without the first or second param being required).

f:func( $req1, $opt1, $opt2, $opt3 )
f:func( $req1, $opt1 )
f:func( $req1, $opt1, $opt2 )

would work, but what about

f:func( $req1, $opt2 )
f:func( $req1, $opt2, $opt3 )
f:func( $req1, $opt3 )

another option would be to use a sequence:

f:func( $req1, $seq1 as item()* )

but it's still difficult to make sense of parameters passed as they
cannot be identified as no name and position is non-significative

the "least evil" solution seems to be for me right now:

f:func( $req1, $params as element(params) )

where $params := <params><opt1>a</opt1><opt2>b</opt2><opt3>c</opt3></params>
advantage here is to have explicit parameter names and it's easy to
make sense of them.
but drawbacks are it's quite more verbose, and all the verification
has to be done by the function body

I haven't really looked at maps (they are planned for XQuery 3.0 I
think), what would their advantage be over the node approach? Any
other ideas or pointers for more information?


More information about the talk mailing list