Hi, Ron and Tim: I think the concern about using xdmp:set() with globals is that side effects are contrary to functional programming (though some compromises are always going to be necessary -- inserting a document in a database is a side effect, after all).
If you create the map in the top function and pass it as a parameter, the map will be visible only within the call stack for that request. One alternative would be to return a sequence containing both the output and the error from each function, but then you can't use strong typing on the function, and you have to use predicates on the returned sequence to separate the output and error. Another alternative would be to return a map in which one key identifies the output and another the error, but then you have to construct a map in each function. Erik Hennum ________________________________________ From: [email protected] [[email protected]] On Behalf Of Tim Meagher [[email protected]] Sent: Thursday, September 15, 2011 8:03 AM To: 'General MarkLogic Developer Discussion' Subject: Re: [MarkLogic Dev General] How to effectively pass variables by reference to an xquery function? Hi Erik, It certainly appears that using a map would effectively allow me to create global variables in the map hash that can be accessed within functions, but if I understand Ron's objections, they have to do with using an xquery extension in the form of xdmp:set. However if I understand correctly, map is also a MarkLogic extension, in which case I would merely be replacing the use of one MarkLogic extension with another. I confess that I regularly use xdmp:set as a simple way of modifying local variables, but the question I have is if using xdmp:set to modify global variables in a function is a supported feature with deterministic results. BTW, isn't the map global in scope? Why or how would you pass it into a function rather than just directly accessing it in the function? Thank you! Tim -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Erik Hennum Sent: Thursday, September 15, 2011 10:33 AM To: General MarkLogic Developer Discussion Subject: Re: [MarkLogic Dev General] How to effectively pass variables by reference to an xquery function? Hi, Tim: Distinguishing the function output from function errors seems like a good idea (witness stdout and stderr). One possibility is to create a map at the top of the call stack and pass the map through to called functions. Called functions can append to an error key in the map. The benefit is having strongly typed function returns and recording errors or warnings without the global side effects that Ron sensibly cautions against. The cost is the map creation, but that's minimal if you create one map per request. Hoping that helps, Erik Hennum ________________________________________ From: [email protected] [[email protected]] On Behalf Of Ron Hitchens [[email protected]] Sent: Thursday, September 15, 2011 7:12 AM To: General MarkLogic Developer Discussion Subject: Re: [MarkLogic Dev General] How to effectively pass variables by reference to an xquery function? XQuery is designed as a functional language. One of the design principles of a functional language is that there should not be side effects. One way of insuring no side effects is to not let variables change their values. You never "set" a variable, you bind a value to it that never changes thereafter. So that means you can't modify a variable through a reference because you can't modify variables. You can in fact mutate variables with xdmp:set. But that's a MarkLogic extension that is not part of XQuery proper. There are uses for xdmp:set, but you should always think twice before using it. Inducing side effects in a side-effect free language is definitely a sharp tool. I would recommend returning an XML fragment with all the values you want your function to produce. On Sep 15, 2011, at 2:45 PM, Tim Meagher wrote: > Hi Folks, > > I am calling a function that returns a value but I also want the > function to set a status as well. I'm wondering what is the best way > to pass by reference? I can return an xml structure that contains the > return value and a status, but I'm also wondering about just using > xdmp:set() to set a global variable in a function. _______________________________________________ General mailing list [email protected] http://developer.marklogic.com/mailman/listinfo/general _______________________________________________ General mailing list [email protected] http://developer.marklogic.com/mailman/listinfo/general _______________________________________________ General mailing list [email protected] http://developer.marklogic.com/mailman/listinfo/general
