[REBOL] Re: local vars in functions
From: lmecir:mbox:vol:cz at: 26-Apr-2002 10:19
I don't think, that the difference between CONTEXT and FUNC is as clear as
Holger states. First of all, I think, that it is a matter of preferences
(which may differ). To demonstrate, that the difference is really small,
let's have a look at the following code sample:
blk: [change at blk 9 first [b:] a: 1 placeholder 2]
c: context blk
Here we see, that the BLK ends up being changed to:
== [change at blk 9 first [b:] a: 1 b: 2]
Which would yield a different result than
make object! [
when used as the BLK argument. My observation is, that the BLK argument is
scanned for local words *before* it is being executed.
To demonstrate a possibility to do something like that for functions, here
is a LOCFUNC function (using some code from
locfunc: function [
"Defines a user function with implicit local words."
body [block!] "The body block of the function"
] [vars sw] [
vars: copy 
parse body [
set sw set-word! (append vars to word! :sw) |
make function! head insert insert tail copy spec /local vars body
, which may be of some use for Rishi n' est ce pas?
f: locfunc  [a: 2]
f ; == 2
a ; == 4
----- Original Message -----
From: "Holger Kruse"
On Thu, Apr 25, 2002 at 10:17:38AM +0200, Ladislav Mecir wrote:
> Although that sounds logical, it is not the whole truth. Compare the way
> USE declares the local variables (the declarations don't appear in the
> argument) vs. the way how CONTEXT declares the local variables (the
> declararions appear in the BLK argument). That surely isn't a non-option.
Yes, it is the whole truth. Read again:
: 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"
: know about all declarations without actually executing the function.
The only reason why 'context allows implicit declarations is because it
executes its body. Functions obviously cannot do that at declaration time.