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

StackOverflow https://stackoverflow.com/questions/17865298

  •  04-06-2022
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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
]

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top