[REBOL] Re: Macro - function problems
From: rotenca:telvia:it at: 13-Mar-2002 1:07
Hi, Mark
> My MACRO function takes three arguments
> which are these
>
> 'name [word!] args [block!] body [block!]
>
> here are some simple examples of what I mean
>
> >> MACRO nil ['word] [set word zero]
> >> nil b
> == (set 'b zero) ; or func [] [set 'b zero]
> >> source b
> b: (set 'b zero) ; or func [] [set 'b zero]
> >> b
> == 0
> >> source b
> b: 0
>
> and another example
>
> >> MACRO printer ['word 'value] [print value]
> >> printer test "Hello"
> == (print "Hello) ; or func [] [print "Hello"]
> >> source test
> test: (print "Hello") ; or func [] [print "Hello"]
A solution (not a guru's one) could be:
macro: func ['w spec body][
set :w func spec compose/deep [set get (first spec) does compose/deep
[(body)]]
]
which must be used with this syntax:
macro printer ['word 'value] [print (value)]
The substitution is made by compose, so the 'value must be between parens,
this limits the use of parens in the body (but there is a workaround:
(to-paren [])).
Another more general solution could be a closure func.
In this example:
>> MACRO nil ['word] [set word zero]
> b: (set 'b zero) ; or func [] [set 'b zero]
the arg 'word is used twice:
1) like the name of word to assign the macro
2) like a value in the macro body
The result is a macro which changes the word which points to itself. I think
should be better to distingue the two.
BTW, with my solution, the nil should be:
macro nil ['word] [set (:word) zero]
nil 'b
source b
b: func [][set 'b zero]
hope this helps
---
Ciao
Romano