# Attaching the same message to several symbols

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:

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:

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.

-
Once I saw it done by cascading \$NewMessage functions, but I can't remember where. If I remember well, it wasn't too difficult. –  belisarius Feb 27 '13 at 17:30

How about doing something as simple as

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


Then

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


Outputs

sym1::msg: I am feeling grumpy.

sym2::msg: I am feeling grumpy.

sym3::msg: I am feeling grumpy.

-