10.13.6.4 class/1 declaration

Synopsis

:- class ClassName.

:- class ClassName = [SlotDef, ...].

:- class ClassName = Super.

:- class ClassName = [SlotDef, ...] + Super + ....

:- class ClassName = term(Term).

:- class ClassName = term(Term, Goal).

:- class ClassName = term(Term, Goal, Essence).

Arguments

ClassName
atom
SlotDef
term
Super
atom

Description

The definition of class ClassName begins with this class/1 directive and ends with the next class/1 directive, the next end_class/[0,1] directive, or the end of the file, whichever comes first. All clauses that look like method definitions within the scope of the class definition (that is, which have one of <-/2, <</2 or >>/2 as the principal functors of their heads) are considered method definitions of the class.

You may provide as many slot definitions (SlotDef) and superclasses (Super) as you like. All superclasses must be previously defined classes.

A slot definition (SlotDef) has the form

     Visibility SlotName:Type = InitialValue

where Visibility and `= InitialValue' are optional.

Visibility is either public, protected, or private. If it is omitted, the slot is private.

SlotName must be an atom.

SlotType must be one of the following:

integer
long signed integer
integer_32
32-bit signed integer
integer_16
16-bit signed integer
integer_8
8-bit signed integer
unsigned
long unsigned integer
unsigned_32
32-bit unsigned integer
unsigned_16
16-bit unsigned integer
unsigned_8
8-bit unsigned integer
float
64-bit floating point number
float_32
32-bit floating point number
atom
Prolog atom.
address
Long address.
term
Prolog term.
Class
Pointer to an instance of Class, which must be a previously defined class.
pointer(Type)
Like address, except that access to this slot yields, and update of this slot expects, a unary term whose functor is Type

InitialValue may be any constant appropriate for the slot's type.

Term, if specified, is any compound Prolog term. Class declarations of any of the last three forms introduce a term class, which defines any term that unifies with Term as an instance of the class being defined.

Goal, if specified, is any Prolog goal. This goal may be used to restrict which terms that unify with Term will be considered to be instance of the class being defined. The default Goal is true. Other than when it is true, Goal will usually share variables with Term.

Essence, if specified, is a list of terms of the form

     Variable:Type

where Variable is a variable apprearing somewhere in Term and Type is one of the possible Slottype types listed above. There should be a Variable:Type pair for every variable in Term. By specifying an essence, you permit much more space- and time-efficient storage of and access to term slots.

Caveat

Note that every class for which you want to be able to create instances must define at least one create method.

Examples

The following class definition is for a class named point, with two public slots, named x and y. Both slots are of type integer and have initial values of 1 and 2, respectively.

     :- class point =
             [public x:integer=1,
              public y:integer=2].
     
     Self <- create.
     :- end_class point.

Because the slots are public, they have get and put methods generated automatically. Because the class has a create method defined, it is possible to create an instance with the command

     | ?- create(point, PointObj).

which creates a point object and binds the variable PointObj to it.

Using the point class, we could create a class, named_point, which has an extra public slot, name.

     :- class named_point =
             [public name:atom] + point.
     
     Self <- create(Name, X, Y) :-
             Self << name(Name),
             Self << x(X),
             Self << y(Y).
     
     :- end_class named_point.

The only way to create a named_point object requires specifying values for all three slots.

See Also

end_class/[0,1]

obj-scl, obj-tcl.


Send feedback on this subject.