Pierre
Pierre

Reputation: 580

in a rebol function which gets a variable as argument, how to retrieve the original variable

I'm debugging a script which doesn't behave like I wished;

I'm writing a small debug utility function, which would print the 'word followed by its value. I wrote this:

debug: func [x] [print rejoin ['x " => " x] wait 0.5 ]

And, in my code, I'd like to call it simply like this:

phrase: "beer is good"
mots: parse phrase " "
debug phrase
    foreach mot mots [
    debug mot
    ;do something...
    ]

and I would dream that it would output on the console something like:

phrase => "beer is good"
mot => "beer"
mot => "is"
mot => "good"

but I can't find a way to retrieve the variable's original name, that is, its name out of the scope of the function.

Upvotes: 0

Views: 70

Answers (2)

Respectech
Respectech

Reputation: 454

If you look at your 'debug function and compare it to the source of '??, you'll see what you would have needed to do differently:

??: func [
    {Prints a variable name followed by its molded value. (for debugging)}
    'name
][
    print either word? :name [head insert tail form name reduce [": " mold name: get name]] [mold :name]
    :name
]

Upvotes: 2

Pierre
Pierre

Reputation: 580

I had not searched enough...

?? variable

does what I needed.

>> ? ??
USAGE:
    ?? 'name 

DESCRIPTION:
     Prints a variable name followed by its molded value. (for debugging)
     ?? is a function value.

ARGUMENTS:
     name -- (Type: any)

Upvotes: 0

Related Questions