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

[REBOL] Re: local vars in functions

From: holger:rebol at: 24-Apr-2002 14:53

On Tue, Apr 23, 2002 at 08:08:15PM -0700, Rishi Oswal wrote:
> One part of REBOL that can feel odd is when i have to > define local variables of a function in the first > block of the function. I find it a bit annoying to > have to add new variables to the list for every little > local variable I use. In addition, I feel it clutters > up the first block and increases function size. In > addition, the way it is currently done could make it > easy to create hard to detect bugs.
It is the result of REBOL being an interpreter, not a compiler. A compiler examines the complete body of a function before generating code, so the variable declaration can be anywhere. For an interpreter there are only two options: 1. Make the context of a function "static", i.e. defined when a function is declared. In that case declarations have to be explicit, i.e. during the execution of "func" or "function", when the function is defined, all variables must be known. If declarations can appear in the body of a function then there is no way for "func" to know a list of all variables. Scanning the function body is not an option, because declarations could be "hidden", e.g. with something like [do rejoin ["a" ": " "1"]], i.e. there is no way for "func" to know about all declarations without actually executing the function. 2. Make the context of a function "dynamic", i.e. make it empty when a function is declared, and extend it at run-time as needed. This is not how REBOL currently works. Allowing this kind of model would require significant changes to the interpreter. It is the same problem as "extending" the context of objects.
> What I would like to see is another shortcut to > creating local variables in any context (function, > innerfunction, loop). The obvious way I see of doing > this is as follows: > > myfunc: func [][ > localvar:: $25
Nooooo. Please do not do that :-). You did not just create a notation with that, you actually created a new datatype: a set-set-word!. We do not like to create new datatypes unless it is really necessary and the exact meaning, scope, usefulness etc. are very well defined and can be clearly demonstrated. Creating a new datatype only to shortcut variable declaration seems ... inappropriate, for lack of a better word :-). -- Holger Kruse [kruse--nordicglobal--com]