6
$\begingroup$

Is there a way to temporarily suppress certain messages, so that I could write for example (with made-up syntax for that feature):

WithOff[Pattern::patv, rule = (f[x_Integer|{x__Integer}] :> g[x])];
rule2 = x_[x__] :> x;

and get no Pattern::patv message for rule, but do get one for rule2 iff the message was enabled at the beginning (that is, WithOff doesn't affect the on/off status of the message outside of its argument)?

$\endgroup$
2
  • 5
    $\begingroup$ Why can't you use Quiet[]? Quiet[rule = (f[x_Integer | {x__Integer}] :> g[x]), Pattern::patv] $\endgroup$ May 22, 2012 at 11:18
  • 1
    $\begingroup$ @J.M.: Because I didn't know that and obviously my search-fu was lacking. $\endgroup$
    – celtschk
    May 22, 2012 at 12:06

2 Answers 2

13
$\begingroup$

I agree completely with J.M., Quiet is the answer.

Implementing WithOff using Quiet is (as I'm sure you know) trivial. Here it is, just for fun:

ClearAll[WithOff]
SetAttributes[WithOff, HoldAll];
WithOff[msg_, expr_] := Quiet[expr, {msg}];
WithOff[Pattern::patv, rule = (f[x_Integer | {x__Integer}] :> g[x])];
rule2 = x_[x__] :> x;
$\endgroup$
12
$\begingroup$

You can define the function:

 messageIsOn[msg_]:=Head[msg]===String

Which yields True if the message is on. Then do e.g:

msgStatus=messageIsOn[Pattern::patv]
If[msgStatus, Off[Pattern::patv]]
< some calculation suppressing message Pattern::patv >
(* Restore the message status *)
If[msgStatus, On[Pattern::patv]]
$\endgroup$
2
  • 3
    $\begingroup$ That's a very interesting solution. While Quiet is obviously the thing I've been looking for, this answer is actually the more interesting. I've never considered that Off might modify the message itself. Interestingly, assignment can be used to switch the message off, but not to switch it on. $\endgroup$
    – celtschk
    May 22, 2012 at 12:15
  • $\begingroup$ @celtschk I use this mainly in packages, where I don't want to suppress all messages, but only those I know to be harmless in this case. $\endgroup$ May 22, 2012 at 15:19

Your Answer

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

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