Yes, it's an extension, but so are try/catch blocks. 

You can bind a map to a global variable (in which case it can be referenced and 
modified by any function), or create and pass maps through functions as a local 
variable just like any other XQuery primitive type, as Erik suggests.  

You can use xdmp:set to modify a global variable, but as Ron noted it does 
sidestep the functional paradigm and is a very sharp tool, it's very easy to 
shoot yourself in the foot if you've got various functions setting it 
concurrently.  

--Colleen

________________________________
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

Reply via email to