Hi All,
I came up with a hack that seems to produce something like the desired
behaviour. The general module looks like this:
xquery version "1.0-ml";
module namespace g = "my-general";
import module namespace p = "my-particular" at "my-p.xqy";
declare function foo() as item()* {
try {
xdmp:apply(xdmp:function(xs:QName("p:foo")))
}
catch ($e) {
"This is g:foo."
}
};
The particular module looks like this:
xquery version "1.0-ml";
module namespace p = "my-particular";
declare function foo() as item()* {
"This is p:foo."
};
If p:foo() is not defined then calling g:foo() produces "This is g:foo."
If p:foo() is defined then calling g:foo() produces "This is p:foo."
The down side is that every over-ridable function in the general module
has to contain a try/catch block. I hear that try/catch introduces
performance issues. Any comments on how much of a performance hit is
caused by using try/catch?
Variable override can be done by putting something like this in the
general module:
declare variable $foo as item()* := ($p:foo, "bar")[1];
and putting something like this in the particular module:
declare variable $foo as item()* := ("baz");
If you don't want to override the general variable definition then put
this in the particular module:
declare variable $foo as item()* := ();
The down side is that every over-ridable variable in the general module
has to be repeated in the particular module, even if it is defined as
the empty sequence.
Best,
Tim Finney
> Hi Everyone,
>
> I'm interested to know how others do extension and override.
>
> Say I have a set of generic library modules that do generally useful
> things that I might use over and over again. Let's call them model.xqy,
> view.xqy, and controller.xqy just for fun. Now, let's say that I also
> have particular versions of these modules, say, model-p.xqy, view-p.xqy,
> and controller-p.xqy which may or may not extend function definitions in
> the corresponding general modules. The particular modules might even
> define entirely new functions.
>
> What I'd like is to be able to use the function interface (signature)
> defined in a general module to constrain the corresponding function in
> the particular module. That is, I want to guarantee that a function in a
> particular module has the same signature as the corresponding one in the
> general module, preferably in some automatic way besides generating an
> error at run time. More importantly, I want to be able to extend or
> override what a general function does by changing the body of the
> corresponding particular function. That is, if there is a definition of
> a function in a particular module then the system knows that it should
> use this definition of the function rather than the corresponding one in
> the general module.
>
> I'd also like the same things to happen with variables: if one is
> defined in a particular module then it must conform to the type of the
> corresponding variable in the general module and the system must know
> that, if a variable is defined in a particular module, the value
> overrides what is in the general module for that variable.
>
> Can it be done in an elegant and bomb proof way? Looking at App Builder
> code gives some clues but I'd like to know whether any of you know about
> good general approaches in this area.
>
> BTW, I want to avoid using xdmp:eval() because the nobody user might be
> using an app built according to this plan.
>
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general