Subject:global variable Author:alain collet Date:21 Mar 2005 01:50 PM
declare variable $XGlobal as xs:integer := 1;
declare function local:ecrireNodeIterXAML($SSTdata, $X, $Y )
{
let $XGlobal := $XGlobal + $X
return $SSTdata
};
let $XGlobal := $XGlobal + 1
let $SSTdata := ""
let $bid := local:ecrireNodeIterXAML($SSTdata, 1, 1 )
I have a need of a real global variable ($XGlobal here) and in this small portion of code, XGlobal in the function is disconnected of the XGlobal value in the main context.
How can I have a real global variable ?
Idea ?
Alain Collet
Subject:global variable Author:(Deleted User) Date:21 Mar 2005 03:07 PM
Hi Alain,
>let $XGlobal := $XGlobal + 1
When you write this expression, you are creating a new XGlobal variable
that has the scope of the FLWOR block, with a value equal to the global
XGlobal variable plus 1.
"[XPath/XQuery] is a functional language. [XPath/XQuery] is built from
expressions, rather than statements. Every construct in the language
(except for the XQuery query prolog) is an expression and expressions
can be composed arbitrarily. The result of one expression can be used
as the input to any other expression, as long as the type of the result
of the former expression is compatible with the input type of the
latter expression with which it is composed. Another characteristic
of a functional language is that variables are always passed by value,
and a variable's value cannot be modified through side effects."
Subject:global variable Author:alain collet Date:22 Mar 2005 01:24 PM
(:Thanks for the answer but ... you can see hereafter more information on my problem :)
declare variable $XGlobal as xs:integer := 1;
declare function local:ecrireNodeIterXAML($SSTdata, $X, $Y )
{
let $XGlobal := $XGlobal + $X
(:here the $XGlobal variable is 2 It seems that the $XGlobal is reinitialiszed to 1 e:)
(: I expected to have a 3+1 = 4 here and not 2 :)
let $XGlobal := $XGlobal + $X
(:here the $XGlobal variable is 3 e:)
return $SSTdata
};
let $XGlobal := $XGlobal + 1
(:here the $XGlobal variable is 2 as I expect and there is only one Global variable in scope:)
let $XGlobal := $XGlobal + 1
(:here the $XGlobal variable is 3 as I expect:)
let $SSTdata := ""
let $bid := local:ecrireNodeIterXAML($SSTdata, 1, 1 )
return $bid
Conclusion
We can change the content of global variable in a scope (main program) but, when we go into a function, the scope seems not to be preserved and the global variable seems to be reinitialized.
your comment ?
Alain
Subject:global variable Author:(Deleted User) Date:22 Mar 2005 01:40 PM
Hi Alain,
what you are seeing is what I explained with the sentence "you are
creating a new XGlobal variable that has the scope of the FLWOR block".
>declare variable $XGlobal as xs:integer := 1;
This creates a global variable with value 1
>declare function local:ecrireNodeIterXAML($SSTdata, $X, $Y )
>{
>let $XGlobal := $XGlobal + $X
>(:here the $XGlobal variable is 2 It seems that the
>$XGlobal is reinitialiszed to 1 e:)
>(: I expected to have a 3+1 = 4 here and not 2 :)
You are creating a new local variable named XGlobal that is equal to
the value of the global variable plus 1, i.e. 2
>let $XGlobal := $XGlobal + $X
>(:here the $XGlobal variable is 3 e:)
Here you are still inside the FLWOR block, so you are creating a new
local variable XGlobal that will be in scope until the end of the FLWOR
block, and its value is that value of the local XGlobal previously
defined plus 1, that is 3.
> return $SSTdata
>};
>
>let $XGlobal := $XGlobal + 1
>(:here the $XGlobal variable is 2 as I expect and there is
>only one Global variable in scope:)
By chance, as you are creating a new local variable named XGlobal with
the value of the global variable XGlobal plus 1, that is 2.
>let $XGlobal := $XGlobal + 1
>(:here the $XGlobal variable is 3 as I expect:)
But you are creating yet another local variable, taking the value of
the previous local variable XGlobal plus 1.
>let $SSTdata := ""
>let $bid :=
>local:ecrireNodeIterXAML($SSTdata, 1, 1 )
>return $bid
>
>Conclusion
>We can change the content of global variable in a scope
>(main program) [...]
>your comment ?
You are not changing the value of the global variable, as XQuery cannot
change the value of any variable (local or global). In the LET statement
you are simply creating new local variables that hides previously
defined variables having the same name. But as soon as you step out
of their scope, XGlobal will be referring to the global variable, that
is always equal to 1.