Function Repository Resource:

ResourceFunctionMessage

Source Notebook

Print a labeled message from ResourceFunction with contexts hidden

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["ResourceFunctionMessage"][symbol::tag]

prints the message symbol::tag unless it has been switched off.

ResourceFunction["ResourceFunctionMessage"][symbol::tag,e1,e2,]

prints a message, inserting the values of the ei as needed.

Details

ResourceFunction["ResourceFunctionMessage"] works like Message, except it attaches the message to ResourceFunction and formats the original symbol without contexts.
ResourceFunction["ResourceFunctionMessage"] generates output on the channel $Messages.
You can switch off a message using Off[symbol::tag]. You can switch on a message using On[symbol::tag].
Between any two successive input lines, the Wolfram Language prints a message with a particular name at most three times. On the last occurrence, it prints the message General::stop.
Off[General::stop] makes the Wolfram Language not stop repeating messages.
During the evaluation of a particular input line, names of messages associated with that input line are appended to the list $MessageList, wrapped with HoldForm. At the end of the evaluation of the nth input line, the value of $MessageList is assigned to MessageList[n].
ResourceFunction["ResourceFunctionMessage"][mname,e1,e2,] is printed as StringForm[mess,e1,e2,] where mess is the value of the message mname. Entries of the form `i` in the string mess are replaced by the corresponding ei.
Given a message specified as symbol::tag, ResourceFunction["ResourceFunctionMessage"] first searches for messages symbol::tag::langi for each of the languages in the list $Language. If it finds none of these, it then searches for the actual message symbol::tag. If it does not find this, it then performs the same search procedure for General::tag. If it still finds no message, it applies any value given for the global variable $NewMessage to symbol and "tag".
If you specify a message as symbol::tag::lang, then ResourceFunction["ResourceFunctionMessage"] will search only for messages with the particular language lang.

Examples

Basic Examples (2) 

Issue a message:

In[1]:=
ResourceFunction["ResourceFunctionMessage"][f::argx, 1, 2]

Define a function to issue a message and return unevaluated upon an error:

In[2]:=
rsqrt[x_] /; If[TrueQ[x >= 0], True, ResourceFunction["ResourceFunctionMessage"][rsqrt::nnarg, x]; False] := Sqrt[x]

Define the message:

In[3]:=
rsqrt::nnarg = "The argument `1` is not greater than or equal to zero.";
In[4]:=
rsqrt[2.25]
Out[4]=
In[5]:=
rsqrt[-2.25]
Out[5]=

Applications (3) 

Here’s a function that prints a message for invalid inputs:

In[6]:=
MyFactorial::positive = "The argument `1` is not a positive integer.";
MyFactorial[0] = 1;
MyFactorial[n_Integer?Positive] := n*MyFactorial[n - 1];
MyFactorial[n_] := (Message[MyFactorial::positive, n]; $Failed);
In[7]:=
MyFactorial[5]
Out[7]=
In[8]:=
MyFactorial[-5]
Out[8]=

When printing messages from a ResourceFunction, symbols can appear with lengthy contexts:

In[9]:=
rf = DefineResourceFunction[MyFactorial]
Out[9]=
In[10]:=
rf[-5]
Out[10]=

Using ResourceFunctionMessage instead of Message will attach the message to ResourceFunction and add a formatted label:

In[11]:=
MyFactorial[
   n_] := (ResourceFunction["ResourceFunctionMessage"][
    MyFactorial::positive, n]; $Failed);
In[12]:=
rf = DefineResourceFunction[MyFactorial]
Out[12]=
In[13]:=
rf[-5]
Out[13]=

Properties and Relations (2) 

Use Quiet to prevent ResourceFunctionMessage from printing messages:

In[14]:=
Quiet[ResourceFunction["ResourceFunctionMessage"][f::argx, 1, 2]; 1 + 1]
Out[14]=

Suppress specific messages:

In[15]:=
Quiet[1/0; ResourceFunction["ResourceFunctionMessage"][f::argx, 1, 2];
  1 + 1, {Power::infy}]
Out[15]=

Possible Issues (2) 

If a message is undefined, a placeholder template is used:

In[16]:=
ResourceFunction["ResourceFunctionMessage"][MyFunction::undefined, f[x], g[y]]

This is similar to the behavior of Message:

In[17]:=
Message[MyFunction::undefined, f[x], g[y]]

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 2.1.1 – 05 April 2021

Related Resources

License Information