[REBOL] Re: 'use question
From: joel:neely:fedex at: 30-Oct-2002 6:05
Hi, Maarten,
After the disclaimer that I think this is a BAAAAAAAD idea...
(That's what objects are for, after all! ;-)
Maarten Koopmans wrote:
> Hi,
>
> COnsider the following code:
>
> f: use [ b ][b: 10 does [ b + 1 ]]
>
> How can I access b later on directly?
>
>> fbody: [[b] b: 10 does [b + 1]]
== [[b] b: 10 does [b + 1]]
>> f: use [b] fbody
>> f
== 11
>> f
== 11
>> f
== 11
(Which, incidentally, makes me wonder if there's a typo...)
Since USE modifies its second argument so that words from the
first argument list are rebound into another context, we take
advantage of the reference to the second argument (block) to
access a dummy placed there just for this purpose.
>> get fbody/1/1
== 10
... and ...
>> set fbody/1/1 23
== 23
>> f
== 24
Of course, variations abound!
As I mentioned above, this seems to be a complex way to avoid
writing a more conventional object, such as:
>> foo: make object! [b: 10 f: func [][b + 1]]
>> foo/f
== 11
>> foo/f
== 11
>> foo/b: 20
== 20
>> foo/f
== 21
Of course, we could make an entry in the Zeroeth Quadrennial
Obfuscated REBOL contest out of this variation:
>> f: get in make object! [b: 10 f: func[][b + 1]] 'f
>> f
== 11
>> f
== 11
HTH (but don't try this in public, kids! ;-)
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]