8.4. Instance Variables

We recall that instance variables (ivars), are non-method items that make up an instance of a class. In the example below, the ivars of the instance math are data and size. To set or query the value of an ivar use the class instance and ivar in dot notation:

Gamma> math.size;
1
	  

Gamma has the ability to add ivars to a class at any time, using the function class_add_ivar. As an example consider the Catalog class used in the above examples. Suppose we would like to have a variable which holds the date when a catalog is started:

Gamma> class_add_ivar(Catalog,#start_date);
nil
Gamma> Catalog;
(defclass Catalog nil [...][data  start_date])
	  

Once an instance variable has been added to a class, all new instances of that class created after the variable was added will receive the new ivar.

Gamma> math;
{Book (data) (size . 0)}
Gamma> cooking = new(Book);
{Book (data) (size . 0) (start_date)}