Class Operators

Class Operators — (., ..)

Syntax

instance.variable = value
instance.variable
instance..variable = value
instance..variable

		

Arguments

instance

An instance of a class.

variable

An instance variable name.

value

A new value to write to the instance variable.

Returns

The value argument.

Description

These operators assign and evaluate object instance values, using familiar C/C++ structure/class reference syntax. The instance and its instance variable are separated by a period and the assignment is made using the = assignment operator. Using two periods between instance and variable makes the reader interpret the instance variable.

Using either the . or the .. without the = assignment operator causes the variable to be evaluated at that instance.

Examples

Gamma> class cmpny { name; address; }
(defclass cmpny nil [][address name])
Gamma> company = new(cmpny);
{cmpny (address) (name)}
Gamma> company.name = "Acme Widgets";
"Acme Widgets"
Gamma> company.name;
"Acme Widgets"
Gamma> var = symbol("name");
name
Gamma> company..var;
"Acme Widgets"
Gamma> 
		

Here is an example of how the .. syntax can be used to allow an instance of one class to access a method of another class. This can be useful if a parent and child widget have different methods with the same name, and you want an instance of one to use the method of the other.

Gamma> class A{}
(defclass A nil [][])
Gamma> class B{}
(defclass B nil [][])
Gamma> class C B{}
(defclass C B [][])
Gamma> method A.get (){princ("Class A's method.\n");}
(defun A.get (self) (princ "Class A's method.\n"))
Gamma> method B.get (){princ("Class B's method.\n");}
(defun B.get (self) (princ "Class B's method.\n"))
Gamma> a = new(A);
{A }
Gamma> a.get();
Class A's method.
t
Gamma> b = new(B);
{B }
Gamma> b.get();
Class B's method.
t
Gamma> (b..A.get)();
Class A's method.
t
Gamma> (a..B.get)();
Class B's method.
t
Gamma> c = new(C);
{C }
Gamma> (c..A.get)();
Class A's method.
t
Gamma> (c..B.get)();
Class B's method.
t
Gamma>