1
$\begingroup$

In a function I'm writing, I need to check whether a particular symbol has been assigned a value (any value) in a particular context. This would work:

ValueQ[box`a]

However, the problem is that the name of the symbol to be checked must be passed to the function as a variable. I've tried passing a string argument name and then doing this:

ValueQ[ToExpression["box`"<>name]]

but that always returns True, presumably because all ValueQ really does is check whether the expression it's given would change if typed into Mathematica, and this one clearly would as the string concatenation and then ToExpression conversion occur. (see this post and similar.)

I can't for the life of me figure out how to programmatically specify the argument to ValueQ in a way that doesn't always produce True. I've tried wrapping the ToExpression with Evaluate, Unevaluated, or Symbol, with no luck. There's always some transformation that happens.

I've also tried checking whether DownValues is empty (which would be good enough for my purposes), like this:

DownValues[ToExpression["box`"<>name]]

and I run into a similar problem: DownValues complains that "Argument ToExpression[box`<>name] at position 1 is expected to be a symbol". I can't figure out how to construct a legitimate symbol from the strings. These produce the same kind of error:

DownValues[ToExpression["box`" <> name, InputForm, Hold]]
DownValues[Symbol["box`" <> name]]

Any suggestions? Thanks...

$\endgroup$
1

Browse other questions tagged or ask your own question.