Wolfram Research

Function Repository Resource:

HasDefinitionsQ

Source Notebook

Check if a symbol has definitions associated with it

Contributed by: Taliesin Beynon (Wolfram Research)

ResourceFunction["HasDefinitionsQ"][sym]

returns True if sym has any top-level values or any Messages.

ResourceFunction["HasDefinitionsQ"]["sym"]

does the same.

Details and Options

HasDefinitionsQ has the attribute HoldAll.
ResourceFunction["HasDefinitionsQ"][sym] returns False if and only if DownValues[sym], OwnValues[sym], ... are all {}.

Examples

Basic Examples

Check if a symbol has definitions associated with it:

In[1]:=
k = 3;
In[2]:=
ResourceFunction["HasDefinitionsQ"][k]
Out[2]=

Check another symbol:

In[3]:=
f[x_] := x^2
In[4]:=
ResourceFunction["HasDefinitionsQ"][f]
Out[4]=
In[5]:=
ResourceFunction["HasDefinitionsQ"]["f"]
Out[5]=

Check another symbol:

In[6]:=
area[square] ^= s^2
Out[6]=
In[7]:=
ResourceFunction["HasDefinitionsQ"][area]
Out[7]=
In[8]:=
ResourceFunction["HasDefinitionsQ"][square]
Out[8]=

Symbols with upvalues give True:

In[9]:=
g /: f[g[x_]] := h[x]
In[10]:=
ResourceFunction["HasDefinitionsQ"][g]
Out[10]=

Symbols with messages give True:

In[11]:=
rlog::nnarg = "The argument `1` is not greater than zero.";
In[12]:=
ResourceFunction["HasDefinitionsQ"][rlog]
Out[12]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information