Re: Name collisions in Monad
- From: ernstgr@xxxxxxxxxxxxxxx (Ernst Gunnar Gran)
- Date: Sat, 3 Dec 2005 02:51:49 +0000 (UTC)
"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
.
- Follow-Ups:
- Re: Name collisions in Monad
- From: James Truher [MSFT]
- Re: Name collisions in Monad
- From: /\\/\\o\\/\\/
- Re: Name collisions in Monad
- References:
- Name collisions in Monad
- From: Ernst Gunnar Gran
- Re: Name collisions in Monad
- From: James Truher [MSFT]
- Name collisions in Monad
- Prev by Date: Re: MSH - Is it possible to show a message box and and get input from user
- Next by Date: Re: Speed-up for load time of MSH
- Previous by thread: Re: Name collisions in Monad
- Next by thread: Re: Name collisions in Monad
- Index(es):
Relevant Pages
|