3
$\begingroup$

Why does the following code not turn off the message Sin::argx?

foo = Sin;
Off[foo::argx];
Sin[4,5]

If I try Evaluate, it gives an error message:

Off[Evaluate[foo]::argx]

Message::name: Message name MessageName[Evaluate[foo],argx] is not of the form symbol::name or symbol::name::language.

Is there a workaround?

$\endgroup$
2
  • $\begingroup$ Why not Off[Sin::argx];? Do you mean you don't want to turn off the Sin::argx, and only want to turn off the message when Sin is stored in foo? Or you simply want to turn off the Sin::argx message without typing Sin? $\endgroup$
    – xzczd
    Dec 27, 2023 at 7:15
  • $\begingroup$ This is part of a much larger code, and there are a number of possible functions that foo can be. $\endgroup$ Dec 27, 2023 at 12:26

1 Answer 1

4
$\begingroup$

Off has an attribute HoldAll, and MessageName has an attribute HoldFirst. To properly evaluate your message name to Sin::argx, you have to Evaluate twice to circumvent both Hold*s:

Quit[];
foo = Sin;

Off[Evaluate[Evaluate[foo]::argx]]
Sin[4, 5]
(* Sin[4, 5] *)

There are other alternatives, such as using With:

With[{foo = foo}, Off[foo::argx]]

or defining a custom function which will evaluate your foo before feeding it into MessageName:

off[f_, tag_] := Off[MessageName[f, tag]]
off[foo, "argx"]

Note, however, that for turning the message back on, the first method with double Evaluate will not work because Sin::argx will get prematurely evaluated to $Off[]. Therefore, use the other two methods:

With[{foo = foo}, On[foo::argx]]

on[f_, tag_] := On[MessageName[f, tag]]
on[foo, "argx"]
$\endgroup$
1
  • $\begingroup$ TYVM, both for the explanation, as well as the workarounds. $\endgroup$ Dec 27, 2023 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.