Hi Tim,

I can think of some ways to do it, but it won't be real overriding, interfacing 
nor inheritance. Most importantly: XQuery only allows overloading with 
different *numbers* of arguments. In OO languages you sometime have to resort 
to delegation as well, that is always an option. But it won't do what you would 
like here I guess.

I think you are looking for a way to use exactly the same from the calling 
side, but simply interchange the called functions. The only way I can think of 
that really approaches this is to define multiple modules within the same 
namespace and only import the one you need in a particular situation. You can 
use delegation for reusable code that you put in a separate, shared module.

Import is rather static though, so you could also use things like xdmp:apply 
(explicitly pointing a particular module), xdmp:eval, xdmp:invoke, etc 
(shielded and amped for security when necessary).

It should be possible though to do a kind of Observer/Listener pattern as well 
using xdmp:apply. Pass in some function name, and optionally some ref id to 
supply the function with some initial context. The 'object' being listened to 
doesn't know who or what is listening, but as long as it takes a known fixed 
number of arguments, it is okay..

HTH..

Kind regards,
Geert


-----Oorspronkelijk bericht-----
Van: [email protected] 
[mailto:[email protected]] Namens Tim Finney
Verzonden: woensdag 10 augustus 2011 15:56
Aan: [email protected]
Onderwerp: [MarkLogic Dev General] Extend and override

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.

Best,

Tim Finney


_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to