It is common in many packages, as well as system symbols, that the same message can be issued by several functions. Here's an example:

Mathematica graphics

One mechanism that can be used for this is to define the message only once, and attach it to the General symbol. This is how the infy messages works as well. infy can appear to come from any symbol:

Mathematica graphics

When writing a custom package, it might not be the best idea to do this though because of the risk of message conflicts. Two packages might attach the same message identifier to General, so the one that's loaded last will overwrite the previous one. In fact, looking at the some standard packages (e.g. CUDALink), they do not attach messages to General even when the same message can be issued by several functions. Looking at the source code of the package, CUDALink uses a helper function to attach the same message text to several symbols in one go.

The question: What is the best way to proceed when several functions need to be able to issue the same message, and indicate that it is coming from them? What do you use in your own packages?

What's important for me is minimizing the risk of conflict with other packages and the simplicity of implementation.

  • 1
    $\begingroup$ Once I saw it done by cascading $NewMessage functions, but I can't remember where. If I remember well, it wasn't too difficult. $\endgroup$ Feb 27, 2013 at 17:30

3 Answers 3


How about doing something as simple as

sym1::msg = "I am feeling grumpy.";
sym2::msg = sym3::msg = sym1::msg; 


Do[With[{ff = f}, 
  Message[MessageName[ff, "msg"]]], {f, {sym1, sym2, sym3}}]


sym1::msg: I am feeling grumpy.

sym2::msg: I am feeling grumpy.

sym3::msg: I am feeling grumpy.

Messages defined for a symbol are stored in list of rules Messages[sym] similar to DownValues, UpValues etc. We can use this fact to create general message inheritance mechanism.

Let's start with defining custom message on our own general symbol:

general::myMessage = "text of general myMessage"

As expected this message is stored in a list as a delayed rule:

(* {HoldPattern[general::myMessage] :> "text of general myMessage"} *)

Now let's try to define a symbol which will inherit messages from our general symbol. First attempt would be:

MessageName[testSym, name_] := MessageName[testSym, name]
(* During evaluation of In[4]:= Message::name: Message name testSym::name_ is not of the form symbol::name or symbol::name::language. >>
$Failed *)

It fails since MessageName is protected from passing non-string as second argument. Also attempt of assigning anything else than explicit string fails:

testSym::myMessage := general::myMessage
(* During evaluation of In[6]:= MessageName::messg: testSym::myMessage cannot be set to general::myMessage. It must be set to a string. >>
$Failed *)

But nothing prevents us from adding hand crafted rule to Messages list:

    HoldPattern[MessageName[testSym, name_]] :> MessageName[general, name]
(* {HoldPattern[testSym::name_] :> general::name} *)

There were no Message::name warnings since none of MessageName expressions was evaluated. The one on left hand side is wrapped in HoldPattern and the one on the right is protected from evaluation by RuleDelayed. They will be evaluated only when called:

MessageName[testSym, "myMessage"]
(* "text of general myMessage" *)

(* During evaluation of In[9]:= testSym::myMessage: text of general myMessage *)

Problem arises when we want to call message from "real" General symbol on our inheriting symbol:

Message[testSym::argctu, testSym]
(* A lot of StringSplit::strse, StringJoin::string, StringForm::string errors *)

It fails since, where Message was expecting a string, it go MessageName[general, argctu], which does not evaluate to a string. We can easily prevent such situations by adding a Condition to our "inheritance" rule, such that rule matches only when MessageName[general, name] evaluates to a string.

    HoldPattern[MessageName[testSym, name_]] :>
        MessageName[general, name] /; StringQ[MessageName[general, name]]
(* {HoldPattern[testSym::name_] :> general::name /; StringQ[general::name]} *)

Now everything works as expected. If message is not defined on testSym, but is defined on general, message from general is used:

(* During evaluation of In[13]:= testSym::myMessage: text of general myMessage *)

If message is not defined neither on testSym nor on general, ordinary delegation to "real" General is used:

Message[testSym::argctu, testSym]
(* During evaluation of In[14]:= testSym::argctu: testSym called with 1 argument. >> *)

If we explicitly define a message on testSym, normal pattern specificity applies and message from testSym is used:

testSym::myMessage = "text of testSym myMessage";
(* During evaluation of In[15]:= testSym::myMessage: text of testSym myMessage *)

Usage in a package


ToyFunc1::usage = ""
ToyFunc2::usage = ""
$ToyVariable::usage = ""
$ToyVariableWithMessages::usage = ""

general::myMessage = "general myMessage"
general::myMessageWithArg = "general myMessageWithArg: `1`"
ToyFunc1::myMessage = "ToyFunc1 myMessage"
ToyFunc2::myMessageWithArg = "ToyFunc2 myMessageWithArg: `1`"
$ToyVariable = "$ToyVariable value"
$ToyVariableWithMessages = "$ToyVariableWithMessages value"

    Quiet[MessageName[sym, name_] =., Unset::norep];

        HoldPattern[MessageName[sym, name_]] :>
            MessageName[general, name] /;
                StringQ[MessageName[general, name]]
] @@@
    (ToExpression[#, InputForm, Hold]&) /@
        DeleteCases[Names["ToyPackage`*"], "$ToyVariable"]


ToyFunc1 inherits from general and overrides myMessage:

(* During evaluation of In[1]:= ToyFunc1::myMessage: ToyFunc1 myMessage *)
Message[ToyFunc1::myMessageWithArg, "arg"]
(* During evaluation of In[2]:= ToyFunc1::myMessageWithArg: general myMessageWithArg: arg *)
Message[ToyFunc1::argctu, ToyFunc1]
(* During evaluation of In[3]:= ToyFunc1::argctu: ToyFunc1 called with 1 argument. >> *)

ToyFunc2 inherits from general and overrides myMessageWithArg:

(* During evaluation of In[4]:= ToyFunc2::myMessage: general myMessage *)
Message[ToyFunc2::myMessageWithArg, "arg"]
(* During evaluation of In[5]:= ToyFunc2::myMessageWithArg: ToyFunc2 myMessageWithArg: arg *)
Message[ToyFunc2::argctu, ToyFunc1]
(* During evaluation of In[6]:= ToyFunc2::argctu: ToyFunc1 called with 1 argument. >> *)

$ToyVariable was explicitly excluded from inheritance:

(* During evaluation of In[7]:= $ToyVariable::myMessage: -- Message text not found -- *)
Message[$ToyVariable::myMessageWithArg, "arg"]
(* During evaluation of In[8]:= $ToyVariable::myMessageWithArg: -- Message text not found -- (arg) *)
Message[$ToyVariable::argctu, $ToyVariable]
(* During evaluation of In[9]:= $ToyVariable::argctu: $ToyVariable value called with 1 argument. >> *)

$ToyVariableWithMessages inherits from general and doesn't override anything:

(* During evaluation of In[10]:= $ToyVariableWithMessages::myMessage: general myMessage *)
Message[$ToyVariableWithMessages::myMessageWithArg, "arg"]
(* During evaluation of In[11]:= $ToyVariableWithMessages::myMessageWithArg: general myMessageWithArg: arg *)
Message[$ToyVariableWithMessages::argctu, $ToyVariableWithMessages]
(* During evaluation of In[12]:= $ToyVariableWithMessages::argctu: $ToyVariableWithMessages value called with 1 argument. >> *)

NDSolve uses NDSolve`NDSolveMessage[] to do something like what @Sascha suggests in his answer. It creates (or loads) the messages on the fly, and defines them for the *NDSolve* functions that use them. Writing such a function should not be hard, but I will show another approach prompted by @belisarius's hint.

The main difference from NDSolveMessage is that it defines the message only for the specified symbol. The function myMessage[g::tag, args...] works like Message, except that if the message g::tag is undefined, it it uses $NewMessage to look for a message foo::tag and defines g::tag = foo::tag (Sascha's method), if it finds one. (One could add an argument msghead to specify which symbol's messages to search instead of a hard-coded foo.)

SetAttributes[myMessage, HoldFirst];
myMessage[m : MessageName[f_, name_], args___] := Block[{
   $NewMessage = Function[{symbol, tag},
       Verbatim[HoldPattern][HoldPattern@MessageName[foo, tag]]],
      MessageName[symbol, tag] = MessageName[foo, tag]
  Message[m, args]

Typical use-case:

foo::murf = "Try something other than ``.";

myMessage[myFunc::murf, 2]

myFunc::murf: Try something other than 2.

myMessage[myFunc::undefined, 2]

The behavior on undefined messages is the same as Message

myFunc::undefined: -- Message text not found -- (2)

Message[myFunc::undefined, 2]

myFunc::undefined: -- Message text not found -- (2)

(*  {HoldPattern[myFunc::murf] :> "Try something other than ``."}  *)

Addendum: An example of the NDSolve`NDSolveMessage behavior. NDSolveMessage defines the message for each relevant function, including NDSolve`ProcessSolutions, NDSolve`Iterate, etc. It defines a message only for the functions that use it (for NDSolve`Iterate but not for NDSolve`ProcessSolutions or vice versa and so forth), but it defines the message for all the selected functions at once. In the code for NDSolve`NDSolveMessage there must be a database of messages and head to attach them to. I haven't been able to find it, though. Some code for NDSolve`* is exposed and can be inspected with GeneralUtilities`PrintDefinitions. For instance, the InitializeMethod[] of NDSolve`ExplicitRungeKutta shows several uses of NDSolveMessage; however no code for NDSolveMessage itself is exposed.

enter image description here


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.