Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Modify any messages generated during an evaluation using a set of replacement rules
ResourceFunction["MessageReplace"][eval,rules] transforms any messages that are issued during evaluation of eval using rules. | |
ResourceFunction["MessageReplace"][eval,rules,replacer] uses the replacement function replacer to make transformations. |
Automatic | choose a replacement function automatically based on rules |
All | equivalent to ReplaceRepeated. |
"type" | a predefined replacement function |
func | an arbitrary replacement function |
"Symbol" | replaces symbol |
"Tag" | replaces tag |
"Parameters" | replaces in the arg1,arg2,… |
Change which symbol a message is issued from:
In[1]:= |
Out[1]= |
Compare to the normal message:
In[2]:= |
Out[2]= |
Replace message tags:
In[3]:= |
In[4]:= |
Replace using patterns:
In[5]:= |
Out[5]= |
Replace using a list of rules:
In[6]:= |
Out[6]= |
Specify a replacement function:
In[7]:= |
In[8]:= |
Replace only the message symbol:
In[9]:= |
Replace only the message tag:
In[10]:= |
In[11]:= |
Replace only in message parameters:
In[12]:= |
When the replacement function is Automatic, the type of replacement differs depending on the rules given. Here are some test messages:
In[13]:= |
Symbol replacements change the message symbol (first argument of MessageName):
In[14]:= |
String replacements change the message tag (second argument of MessageName):
In[15]:= |
Mixing rules results in replacement across the whole message (including parameters):
In[16]:= |
Define a function that takes ownership of messages issued:
In[17]:= |
In[18]:= |
Out[18]= |
See the expression structure that's passed to the replacement function:
In[19]:= |
Out[19]= |
If the target symbol and tag is not a defined message, it will be defined temporarily in order to print the message:
In[20]:= |
In[21]:= |
The temporary message definition is cleared:
In[22]:= |
Out[22]= |
If the message is already defined for the target symbol and tag, it won't be changed:
In[23]:= |
In[24]:= |
In[25]:= |
Out[25]= |
Changing the target message template can result in additional errors if the number of template slots increases:
In[26]:= |
In[27]:= |
Compare to the original:
In[28]:= |
If the number of template slots decreases, some parameters will be dropped:
In[29]:= |
Compare to the original:
In[30]:= |
The original message is never issued, so inner code that checks for specific messages may give unexpected results:
In[31]:= |
Out[31]= |
MessageReplace does not affect quieted messages, so the underlying code will work as expected:
In[32]:= |
Out[32]= |
Replace numbers in messages using IntegerName:
In[33]:= |
Use replacement rules to temporarily define alternate behavior for messages:
In[34]:= |
Out[34]= |
This work is licensed under a Creative Commons Attribution 4.0 International License