Re: Name collisions in Monad

Tech-Archive recommends: Speed Up your PC by fixing your registry



"James Truher [MSFT]" <jimtru@xxxxxxxxxxxxxxxxxxxx> writes:
>
> We spent a lot of time working on the scoping rules in Monad. Scripts
> create a new scope when run, and variables _created_ in that new scope are
> tossed when the script exits. If you want to affect the global scope, you
> can use $global:variable which is similar to tcl upvar. However, in
> general, you shouldn't need to worry about scopes too much, unless you have
> specific requirements.

[...loads of nice doc...]


Hi

First of all thx alot for the updated doc. I found it very helpful - easy reading
and to the point (with nice examples)!

While playing with scoping I came across this rather strange(?) behavior. Would be
nice if u are able to explain why this happens...

I got this file I dot source into the shell:
(Don't ask how I came up with this - it's just to show what I find strange :)

=============== Strange.msh =================

$global:bookkeeping = 0

function beNice {
$global:bookkeeping += 1
"Inside a nice function: " + $bookkeeping
}

function strange {
$bookkeeping += 1
"Inside a strange function: " + $bookkeeping
}

function strange2 {
$bookkeeping += 1
"Inside another strange function: " + $bookkeeping

}

===========================================


In a fresh shell I do the following (numbering for easy ref at the bottom) :

===========================================
1] MSH C:\Scripts> . .\Strange.msh
2] MSH C:\Scripts> beNice
3] Inside a nice function: 1
4] MSH C:\Scripts> beNice
5] Inside a nice function: 2
6] MSH C:\Scripts> beNice
7] Inside a nice function: 3
8] MSH C:\Scripts> strange
9] Inside a strange function: 4
10] MSH C:\Scripts> strange
11] Inside a strange function: 1
12] MSH C:\Scripts> strange
13] Inside a strange function: 1
14] MSH C:\Scripts> beNice
15] Inside a nice function: 4
16] MSH C:\Scripts> beNice
17] Inside a nice function: 5
18] MSH C:\Scripts> strange2
19] Inside another strange function: 6
20] MSH C:\Scripts> strange2
21] Inside another strange function: 1
22] MSH C:\Scripts> beNice
23] Inside a nice function: 6
24] MSH C:\Scripts> strange2
25] Inside another strange function: 1
26] MSH C:\Scripts>
===========================================

Line 1-7 are straigh forward. At the first call to 'strange' (line 8)
the right hand side (RHS) of the assignment (first line in the 'strange' function)
is evaluated before the local $bookkeeping is created. The RHS evaluates
to 4 and the local $bookkeeping gets the value 4 (which is correct as far as
I understand the rules of scoping in Monad). The value 4 is given as output.
When the functions ends the local $bookkeeping variable is gone. But look
what happens when I do a second call to 'strange' at line 10. Now the
RHS of the first line in the 'strange' function evaluates to 1 (even if
the global $bookkeeping variable still got the value 3). Why does this
happen? A third call of the 'strange' function (line 12) gives the same
result. Somehow the function suddenly starts using a fresh(?) local
$bookkeeping variable (with the value 0) when evaluating the RHS of the
assignment? This is not what I would expect. New calls to the 'beNice'
function (line 14-17) confirms that the global $bookkeeping is not changed by
the 'strange' function (the value is 4, not 5, at line 15 as the calls to
'strange' never did alter it). Line 18-21 shows the same behavior when calling
the 'strange2' function and line 22-25 shows that an inbetween call to the
'beNice' function doesn't change the behavior of 'strange2'; the first time
ever the function is called it behaves different from the rest of the calls.

Why does this happen? I find it a bit confusing. Is it a bug? Would be nice if
u had the time to write some words about this :-)


Best Regards,
Ernst Gunnar Gran

.



Relevant Pages

  • Re: MSH-change directory bug
    ... line-where extracted files will going?Not current directory but in msh ... dir!What's very strange and confusing and totally not compatible with ...
    (microsoft.public.windows.server.scripting)
  • Re: Amps and Ohms
    ... not too many folks know it was an audio oscillator. ... audio test gear. ... watch the waveform for anything strange during testing. ... I have seen a tech try to measure power on a scope itself, ...
    (rec.audio.car)
  • identifier out of scope
    ... I'm getting what I think is a very strange result in ASP.NET. ... as soon as I get past the first line of code and the debugger is ready to ... identifier 'tempCol' out of scope". ... I'm checking into the twilight zone!!! ...
    (microsoft.public.dotnet.general)
  • Re: Name collisions in Monad
    ... this is indeed a bug. ... Each time the strange and ... encompassing scope. ... > Monad Program Management ...
    (microsoft.public.windows.server.scripting)
  • Re: Name collisions in Monad
    ... the right hand side (RHS) of the assignment is evaluated before the local $bookkeeping is created. ... When the functions ends the local $bookkeeping variable is gone. ... But look what happens when I do a second call to 'strange' at line 10. ... I would gladly change the "Correct"-behavour, of the first call, copying the $global variable (if I write a script, I would not know if somebody already got's a $bookkeeping in his $global scope so this could be "un-expected" and hence unwanted for the script.(we can still ...
    (microsoft.public.windows.server.scripting)