[REBOL] Re: local vars in functions
From: lmecir:mbox:vol:cz at: 27-Apr-2002 8:39
Hi,
(resending my yesterday's mail)
I don't think, that the difference between CONTEXT and FUNC is big.
First of all, I think, that it is a matter of preferences.
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
probe blk
probe c
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! [
a: 1
]
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
http://www.rebolforces.com/~ladislav/contexts.html ):
locfunc: function [
{Define a function with set-words handled implicitly as local.}
spec [block!]
body [block!] "The body block of the function"
] [vars sw] [
vars: copy []
parse body [
any [
set sw set-word! (append vars to word! :sw) |
skip
]
]
make function! head insert insert tail copy spec /local vars body
]
, which may be of some use for Rishi, n' est ce pas?
Usage:
a: 4
f: locfunc [] [a: 2]
f ; == 2
a ; == 4
Cheers
L
----- Original Message -----
From: "Holger Kruse"
On Thu, Apr 25, 2002 at 10:17:38AM +0200, Ladislav Mecir wrote:
> </Holger>
>
> Although that sounds logical, it is not the whole truth. Compare the way
how
> USE declares the local variables (the declarations don't appear in the
BODY
> 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"
: to
: 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.
--
Holger Kruse
[kruse--nordicglobal--com]