DeclareClass

DeclareClass[]
DeclareClass

DetailsDetails

  • A declared class is used to store properties, rules, and definitions for instances of that class.
  • DeclareClass it typically evaluated in a context reserved specifically for class definitions.
  • ClassQ[class] returns True for a symbol class that has been successfully declared a class using DeclareClass.
  • DeclareClass[class] and DeclareClass[class,parent] only work if class is not yet a declared class, and if it does not have the attribute Protected. parent has to be a declared class.
  • Instances of class class are internally stored as class[assoc], with assoc an Association object.
  • Default values for class elements can be added with DeclareDefaults[class,defaults], but only after class has been declared using DeclareClass.
  • Invariants for class can be added with DeclareInvariant[class,invariant], but only after class has been declared using DeclareClass.
  • The class property property for a declared class class can be requested using class[property].
  • DeclareClass sets up the following basic class properties for the symbol class:
  • "
  • Base
  • "
    the base class
    "
  • Class
  • "
    the class symbol
    "
  • ClassDefaults
  • "
    the defaults specified for the class
    "
  • ClassInvariant
  • "
  • the invariant specified for the class
  • "
  • Defaults
  • "
    the defaults specified for the class and its parents
    "
  • Format
  • "
    the format rules for the class
    "
  • Instance
  • "
    a default instance
    "
  • Invariant
  • "
    the invariant specified for the class and its parents
    "
  • Parent
  • "
    the parent symbol
    "
  • ParentDefaults
  • "
    the defaults specified for the class parents
    "
  • ParentInvariant
  • "
    the invariant specified for the class parents
    "
  • Parents
  • "
    list of all class parents
    "
  • Properties
  • "
    list of all properties
  • Once a symbol class is a declared class, class instances can be created by evaluating class[elements], with elements a rule, list of rules, or Association object.
  • Part can be used to retrieve values in a class instance as a shorthand for Retrieve, and, in combination with ClassSet or ClassSetDelayed, modify values in a class instance.
  • Normal[instance] returns an Association object with the elements stored in the class instance instance.
  • The instance property property can be requested from an instance instance using instance[property].
  • Instance and class properties can be defined and redefined using Set and SetDelayed.
  • Formatting rules, default values, instance properties, and class properties are all forwarded to a child class' parent, unless they have been redefined.
  • Class symbols by default have the attribute ReadProtected.
  • Class symbols can be protected from unintended changes with the usual Protected, ReadProtected, and Locked attributes.

ExamplesExamplesopen allclose all

Basic Examples  (1)Basic Examples  (1)

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
In[3]:=
Click for copyable input
In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=