Constant

Constant.method
marks a method in a class that may not change the related object or class.
  • The builtin behavior of Constant is not affected.
This loads the package.
In[1]:=
Click for copyable input
We define a class with a normal method modify and a constant one as well as a field. Static members avoid the necessity of object generation.
In[2]:=
Click for copyable input
Without the Constant qualifier, a method may change the field.
In[3]:=
Click for copyable input
Out[3]=
The constant method fails. Look for Mutable, if fields must be changed inside a constant method.
In[4]:=
Click for copyable input
Out[4]=
Object bound members work analogously. We start with the class definition.
In[5]:=
Click for copyable input
Again, the unqualified method may change, while the Constant one cannot.
In[6]:=
Click for copyable input
Out[6]=