[REBOL] Combining a word and a value in one argument Re:
From: jkinraid:clear at: 27-Sep-2000 15:24
Hi Tim,
> It would be great if I could have a function that
> would take a rebol word as an argument and
> print out both the word and it's value:
> i.e:
> test-int: 4
> tst test-int
> >>test-int: 4
>
> This would be similar to a c function using the preprocessor
> stringizer
>
> as in #define PRINT(x) print(x,#x)
> print(int x,char* x_name)
> {
> return printf("%s: %d",x_name,x);
> }
> test-int = 4
> PRINT(test-int); // gives test-int: 4
> I tried playing with first system/words but couldn't come up
> with anything consistant.
> TIA
> -Tim
>> test-int: 4
== 4
>> ?? test-int
test-int: 4
== 4
>> source ??
??: func [
{Prints a variable name followed by its molded value. (for
debugging)}
'name
][
print either word? :name [rejoin [name ": " mold name: get name]]
[mold :name]
:name
]
The trick is that you use a lit-word! as an argument for a function.
That way, the argument isn't evaluated.
>> tst: func ['one two] [print [one two]]
>> a: 1
== 1
>> b: 2
== 2
>> tst a b
a 2
So you use the 'get function to find out what the value is.
>> tst: func ['one two] [print [get one two]]
>> a: 1
== 1
>> tst a b
1 2
But that will fail if you don't pass a word! as an argument.
>> tst 1 2
** Script Error: get expected word argument of type: any-word.
** Where: get one two
So all that is needed is some code to check if the argument is a word or
not.
>> tst: func ['one two] [print [either word? :one [get one] [one] two]]
>> tst a b
1 2
>> tst 3 4
3 4
The rest of the code in the ?? function just deals with molding the
value.
Have fun,
Julian Kinraid