Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Set up a symbol to give an error message when called with an unexpected number of arguments
ResourceFunction["DeclareArgumentCount"][symbol,n] specifies that symbol is a function that takes exactly n arguments. | |
ResourceFunction["DeclareArgumentCount"][symbol,{min,max}] specifies that symbol takes between min and max arguments. |
Declare that a symbol myFunc takes exactly two arguments:
In[1]:= |
When called with the correct number of arguments, the function behaves as normal:
In[2]:= |
Out[2]= |
In[3]:= |
Out[4]= |
Attempting to evaluate myFunc with the incorrect argument count throws an error message and does not evaluate:
In[5]:= |
Out[5]= |
DeclareArgumentCount does its work by defining a downvalue for the symbol in question:
In[6]:= |
Out[8]= |
The downvalue created by DeclareArgumentCount can be superseded by other downvalues in accordance with normal evaluation rules:
In[9]:= |
Now a two-argument call does not give a message despite having declared myFunc3 to take three arguments:
In[10]:= |
Out[10]= |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License