Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: The truth about scope

From: gabriele:colellachiara at: 14-Apr-2005 12:29

Hi Bernie, On Wednesday, April 13, 2005, 5:40:36 PM, you wrote: BS> Yes, it's much clearer now. You did a great job. Please continue BS> with contexts and bindings. Ok, so to summarize: The is no scope. Everything is data, and the relation between words and values is data too. You can manipulate this relation in the same way you can manipulate the rest of the data. So, the "connection" (binding) between words and their referred values is not a "rule" in the language, like "scope" is, but it is a property of words. Now... what is a context? And what does binding mean? You can think of a context as a table of words and values. Nothing special here - you have words and their "meaning" listed in some kind of table. But, if words directly refer to values, why do we need contexts? The reason we need them, is that words actually refer to values with an additional layer of indirection. A "double pointer" if you want to call it this way. Why? Because otherwise each word instance would have a different reference. Changing one word would not change all the other words with the same spelling. So we have something like: some-word -----------\ ()---------> value some-word -----------/ some-other-word ----->()---------> value The () in the above picture are contained in the second column of contexts. When you write: some-word: 3 you are not changing the leftmost arrow in the above picture, you are changing the rightmost arrow. Basically you are not changing the word, you are changing the context. Setting the value of a word is an operation on contexts, not an operation on words. Words stay unchanged. Now, the scoop: the leftmost arrow is what is called "binding". The operation of binding is the operation of changing that arrow to make it point to some other (). So: everything is data. Words and their relation to other values are data too. What BIND does is take every word in a block and change it so that it refers to a different context's value slot. What gives the illusion of scope? As Ladislav said, when words are loaded (LOAD), they are bound to the global context. Each time a word is created, a value slot for that word is created in the global context (SYSTEM/WORDS), and this value slot is initialized to the UNSET! value. So, every word starts out being bound there. When code is evaluated, some functions may change the binding of words. Actually, many functions do that. What you get in the end is the illusion of scope, because usually those function are applied on blocks that are nested inside each other. However, this is not a requirement, and this usually confuses people, because they are fooled by the illusion of scope. The "lookup" in the context table is only done at binding time. Once the word has been bound, it "directly" (actually, with a double pointer ) refers to the value, so that when you GET the value of a word no lookup is necessary. This has the nice side effect of making REBOL faster, too. This is why we usually call it "static binding". The connection between words and value is a "static" data structure, not the result of a dynamic computation at runtime. Of course, you can change this structure dynamically, like you can change the contents of a block dynamically. Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amiga Group Italia sez. L'Aquila --- SOON: http://www.rebol.it/