Hello [EMAIL PROTECTED],

Well, scope in REBOL is a quite different beast than in most other languages.

In REBOL you don't only have two scopes (contexts) - you have a tree of contexts, each 
one with a parent context, the top level context being what you could call global 
variables.

REBOL doesn't have the traditional concept of variables either, it has words that, 
depending on which context they're bound to, can evaluate to a value.

Consider the following script:

REBOL []
a: 1
b: 2
c: 3
x: func [/local a] [
    a: 10
    y: func [/local b] [
        b: 20
        z: func [] [
            print [a b c]
        ]
        z
    ] 
    y
]
x
print [a b c]
---

The result is:
10 20 3
1 2 3

As you can see, contexts are traversed bottom-up to find the value of a word.
ie. to lookup 'a in 'z, the word will first be searched in 'z's context, then in 'y 
until it is found in 'x.
PHP's approach wouldn't work here, because what is "global" and how would you refer to 
a parent context?

Most of the time, this is transparent to the scripter, because it mostly works as 
you'd expect.

The problem arise when you "forget" to declare a word local and end up (re)defining a 
"global" or parent word.

Another thing is that, REBOL's contexts are currently static (except the top-level 
one).
Once a context is defined, you can't add or remove words from it.

Having code in the prototype of an object! might give an illusion of this, but look at 
this:

REBOL []
obja: make object! [
    a: 10
    probe self
    b: 20
]

I hope that future REBOL releases will loosen up on this in some way or another.


Best regards
Thomas Jensen

On 19-Jul-00, [EMAIL PROTECTED] wrote:

> I've been wondering what the reasoning behind REBOL's scoping rules was for
> awhile. In C, for instance, any variable that you declare in a function is
> "automatic" (that's what C calls them anyway :). They're automatically local
> to the function they're defined in, etc.
> 
> Why does REBOL have variables be globally scoped by default? I ran into this
> a few days ago when I asked the list for assistance, and people helpfully
> replied (thank you). One of the things people pointed out was that I wasn't
> being careful with my recursion and kept using the same global variable over
> and over again.
> 
> Perl takes a similar approach, where everything is global unless you declare
> the variable 'local' or 'my', but I always assumed that this was just
> because of Perl's heritage as a "throw away" script language, in which cases
> you really don't have to worry about scoping so much.
> 
> PHP takes a completely opposite approach. Any variable you declare is local
> to a function, and you can't even get at global variables unless you
> explicitly get the variable through the $GLOBALS associative array, or
> declare a variable 'global' in your function.
> 
> Anyway, I'd really like to understand why REBOL works the way it does in
> this respect, so if anyone has any insight to give I'd love to receive it.
> Thanks so much.
> 
> Keith
> 
> 


Reply via email to