(lispkit object)
Library (lispkit object)
implements a simple, delegation-based object system for LispKit. It provides procedural and declarative interfaces for objects and classes. The class system is optional. It mostly provides means to define and manage new object types and construct objects using object constructors.
Introduction
Similar to other Scheme and Lisp-based object systems, methods of objects are defined in terms of object/class-specific specializations of generic procedures. A generic procedure consists of methods for the various objects/classes it supports. A generic procedure performs a dynamic dispatch on the first parameter (the self
parameter) to determine the applicable method.
Generic procedures
Generic procedures can be defined using the define-generic
form. Here is an example which defines three generic methods, one with only a self
parameter, and two with three parameters self
, x
and y
. The last generic procedure definition includes a default
method which is applicable to all objects for which there is no specific method. When a generic procedure without default is applied to an object that does not define its own method implementation, an error gets signaled.
Objects
An object encapsulates a list of methods each implementing a generic procedure. These methods are regular closures which can share mutable state. Objects do not have an explicit notion of a field or slot as in other Scheme or Lisp-based object systems. Fields/slots need to be implemented via generic procedures and method implementations sharing state. Here is an example explaining this approach:
This is a function creating new point objects. The x
and y
parameters of the constructor function are used for representing the state of the point object. The created point objects implement three generic procedures: point-coordinates
, set-point-coordinates
, and object->string
. The latter procedure is defined directly by the library and, in general, used for creating a string representation of any object. By implementing the object->string
method, the behavior gets customized for the object.
The following lines of code illustrate how point objects can be used:
Inheritance
The LispKit object system supports inheritance via delegation. The following code shows how colored points can be implemented by delegating all point functionality to the previous implementation and by simply adding only color-related logic.
The object created in function make-colored-point
inherits all methods from object super
which gets set to a new point object. It adds a new method to generic procedure point-color
and redefines the object->string
method. The redefinition is implemented in terms of the inherited object->string
method for points. The form invoke
can be used to refer to overridden methods in delegatee objects. Thus, (invoke (super object->string) self)
calls the object->string
method of the super
object but with the identity (self
) of the colored point.
The following interaction illustrates the behavior:
Objects can delegate functionality to multiple delegatees. The order in which they are listed determines the methods which are being inherited in case there are conflicts, i.e. multiple delegatees implement a method for the same generic procedure.
Classes
Classes add syntactic sugar, simplying the creation and management of objects. They play the following role in the object-system of LispKit:
A class defines a constructor for objects represented by this class.
Each class defines an object type, which can be used to distinguish objects created by the same constructor and supporting the same methods.
A class can inherit functionality from several other classes, making it easy to reuse functionality.
Classes are first-class objects supporting a number of class-related procedures.
The following code defines a point
class with similar functionality as above:
Instances of this class are created by using the generic procedure make-instance
which is implemented by all class objects:
Each object created by a class implements a generic procedure object-class
referring to the class of the object. Since classes are objects themselves we can obtain their name with generic procedure class-name
:
Generic procedure instance-of?
can be used to determine whether an object is a direct or indirect instance of a given class. The last two lines above show that pt2
is an instance of point
, but pt
is not, even though it is functionally equivalent.
The following definition re-implements the colored point example from above using a class:
The following lines illustrate the behavior of colored-point
objects vs point
objects:
Procedural object interface
Symbol representing the object
type. The type-for
procedure of library (lispkit type)
returns this symbol for all objects created via object
or make-object
.
Returns #t
if obj is an object as defined by this library. Objects are either created procedurally via make-object
or declaratively via object
.
Declarative object interface
Procedural class interface
Symbol representing the class
type. The type-for
procedure of library (lispkit type)
returns this symbol for all class objects.
Returns #t
if obj is a class object, #f
otherwise.
root [object]
The root class object. All class objects have root
as its direct or indirect superclass object.
Returns a new class whose name is name. superclasses is a list of superclass objects. constructor is a procedure that is called whenever instances of this new class are being created. constructor are passed the arguments passed to make-instance
when a new object of this class is being created. It returns two values: a list of delegate objects and an initializer procedure which sets up the internal state.
Instance methods
Returns the class of object obj.
Returns #t
if obj and other are considered equal objects.
Returns a string representation of object obj.
Class methods
Returns the class name of class.
Returns a list of superclass objects of class.
Returns #t
if class is a subclass of class other, #f
otherwise.
Creates and returns a new object of class. arg ... are the constructor arguments passed to the constructor of class.
Returns #t
if obj is an instance of class.
Declarative class interface
Last updated