Search code examples
marklogicmarklogic-8

How to avoid passing of map as reference in MarkLogic?


I am using ML 8.0-8

Is there any way to not to pass Map to a function as a reference.

I mean if I make any update in a map in the function it should not reflect in the actual map.

Example:

function call($map as map:map) {
     'add one more key in the $map'
}

declare $actualMap
call($actualMap)
print $actualMap

Updates in the call function should not reflect in the $actualMap


Solution

  • You'll basically have to clone it. You can do that with a one-liner in XQuery, by serializing and parsing it:

    let $clonedMap := map:map(document{ $map }/*)
    

    Note though that a map:map could hold items that cannot be serialized, like function references. In that case, you need to reconstruct the map:map. For instance with:

    let $clonedMap := map:new(map:keys($map) ! map:entry(., map:get($map, .)))
    

    The second method might actually be faster, but it doesn't process a map:map recursively, while the first method would. You could wrap the second in a recursive function with a typeswitch to compensate.

    HTH!