Basically let is a guaranteed evaluation of every variable.  Declare variables 
are only evaluated when used so if you declare a variable but never use it, 
then it will never be evaluated or executed.

Because of the lazy evaluation, declare variables will always be executed 
sequentially so using declare variables effectively eliminates parallelism of 
variable evaluation.

Will Sawyer does did an excellent evaluation of this with examples that show 
the differences between declare and let variables: 
http://www.xquerycoder.com/2014/04/variable-performance-declared-vs-let.html

-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Retter, Adam 
(RBI-UK)
Sent: Thursday, June 19, 2014 9:55 AM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] Evaluation of `declare variable` vs `let`

In this instance we are accessing the variable, and expecting it to cause a 
side effect. The access however is by passing it into 
`xdmp:set-request-content-type` which itself causes a side-effect, could it be 
perhaps that that is lazily evaluated and so the whole thing is eliminated by 
the optimiser?

-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Whitby, Rob
Sent: 19 June 2014 16:50
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] Evaluation of `declare variable` vs `let`

declare variable Š is lazily evaluated, which is great unless you never access 
the variable but expect it to cause a side-effect.


On 19/06/2014 16:28, "Retter, Adam (RBI-UK)" <[email protected]> wrote:

>Given a main module with no user defined functions, is it considered 
>better practice to use `declare variable` as opposed to `let`, the 
>reason I ask is twofold:
>
>1) I have started on a large code-base that seems to eschew `let` in 
>favour of using `declare variable`. This seems somewhat strange to me.
>
>2) We have seen different evaluation strategies, whereby a function 
>which returns an empty-sequence which is bound in a declare variable 
>clause, is never executed. Yet when we re-write that as a let binding, 
>the code is executed. I think perhaps the query optimiser in ML is 
>being too aggressive here?
>
>
>For example -
>
>declare variable $content-type as xs:string := 
>slib:validated-bulk-load-content-type();
>
>and then a few lines later we have -
>
>slib:normal-bulk-load-response ($etag-map, fn:count 
>($etag-map/store:etag-entry), $content-type)
>
>Subsequently the `slib:normal-bulk-load-response` calls 
>xdmp:set-response-content-type($content-type). However the 
>`slib:validated-bulk-content-type()` function is never evaluated, we 
>are certain of this because it eventually calls `fn:error`, yet the 
>error never occurs!
>
>If we switch the `declare variable $content-type` for a `let 
>$content-type` then we do see the error occurring!
>
>Cheers Adam.
>
>DISCLAIMER
>This message is intended only for the use of the person(s) ("Intended
>Recipient") to whom it is addressed. It may contain information, which 
>is privileged and confidential. Accordingly any dissemination, 
>distribution, copying or other use of this message or any of its 
>content by any person other than the Intended Recipient may constitute 
>a breach of civil or criminal law and is strictly prohibited. If you 
>are not the Intended Recipient, please contact the sender as soon as possible.
>Reed Business Information Limited. Registered Office: Quadrant House, 
>The Quadrant, Sutton, Surrey, SM2 5AS, UK.
>Registered in England under Company No. 151537
>
>_______________________________________________
>General mailing list
>[email protected]
>https://urldefense.proofpoint.com/v1/url?u=http://developer.marklogic.c
>om/mailman/listinfo/general&k=wlPCrglRP6kzT4RbABWMaw%3D%3D%0A&r=2FOxwjX
>kcRFP9Zb5gsGqutGbMyYaH6V5O1y2qyDOE%2Bw%3D%0A&m=6US1taeZp7i1l09DW%2FM0PK
>LQmdfcmd4UQuAeQhZUfUw%3D%0A&s=0d1dc97bd878f51ace89506c888d59cb924b91695
>7893db4a0893ee8fcb351e3

_______________________________________________
General mailing list
[email protected]
https://urldefense.proofpoint.com/v1/url?u=http://developer.marklogic.com/mailman/listinfo/general&k=wlPCrglRP6kzT4RbABWMaw%3D%3D%0A&r=2FOxwjXkcRFP9Zb5gsGqutGbMyYaH6V5O1y2qyDOE%2Bw%3D%0A&m=6US1taeZp7i1l09DW%2FM0PKLQmdfcmd4UQuAeQhZUfUw%3D%0A&s=0d1dc97bd878f51ace89506c888d59cb924b916957893db4a0893ee8fcb351e3
_______________________________________________
General mailing list
[email protected]
https://urldefense.proofpoint.com/v1/url?u=http://developer.marklogic.com/mailman/listinfo/general&k=wlPCrglRP6kzT4RbABWMaw%3D%3D%0A&r=2FOxwjXkcRFP9Zb5gsGqutGbMyYaH6V5O1y2qyDOE%2Bw%3D%0A&m=6US1taeZp7i1l09DW%2FM0PKLQmdfcmd4UQuAeQhZUfUw%3D%0A&s=0d1dc97bd878f51ace89506c888d59cb924b916957893db4a0893ee8fcb351e3


 NOTICE: This email message is for the sole use of the intended recipient(s) 
and may contain confidential and privileged information. Any unauthorized 
review, use, disclosure or distribution is prohibited. If you are not the 
intended recipient, please contact the sender by reply email and destroy all 
copies of the original message.

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

Reply via email to