[REBOL] Re: How can one use named constants in switch?
From: joel:neely:fedex at: 25-Oct-2001 15:01
Hi, David,
David Hawley wrote:
> Thanks for the great replies everybody. Now I have another
> question: Is there a way to pass data into predefined cases?
>
> cases: reduce [
> ca [print ["five:" msg]]
> cb [print ["eight:" msg]]
> ]
>
> switch first msg cases
>
> This isn't that big of a deal since I don't really have to
> predefine the cases... but I figure that I'll learn more
> re REBOL
>
I may have misunderstood, but you don't have to do anything to
put data "into" the nested blocks. REDUCEing a block leaves
nested blocks alone, so words in the nested blocks are not
evaluated (until called for).
I don't think I said that very well, but look at this example:
>> set [ca cb cc cd] ["A" "B" "C" "D"]
== ["A" "B" "C" "D"]
>> cases: reduce [
[ ca [print ["five:" msg]]
[ cb [print ["eight:" msg]]
[ ]
== ["A" [print ["five:" msg]] "B" [print ["eight:" msg]]]
Notice that the embedded occurrences of MSG (and of PRINT, for
that matter) aren't yet evaluated. In fact MSG wasn't even
defined at that point. Now I can say:
>> msg: ["B" 42 "This is a test"]
== ["B" 42 "This is a test"]
to give MSG a value, and then say:
>> switch first msg cases
eight: B 42 This is a test
to cause evaluation of an expression for the current value
of MSG.
HTH!
-jn-
--
This sentence contradicts itself -- no actually it doesn't.
-- Doug Hofstadter
joel<dot>neely<at>fedex<dot>com