2005/5/10

     
 

ACDK Lisp Classes

artefaktur

The ACDK Lisp Object system is similar to the Common Lisp Object System.


Content of this chapter:

     Define a Class in Lisp
       Slots
     Create a Class in Lisp
     Accessing Slots
     Object Methods



 Define a Class in Lisp



(defclass ClassName (SuperClasses) 
 (SlotDefinitions)
)
  • defclass: keyword for define a class
  • ClassName: a name for the new class
  • (SuperClasses): List of previosly defined classes.
  • (SlotDefintions): List of definitions of slots (member/methods).

 Slots

In notation of the Common Lisp Object System (CLOS) Slots are members of a class.

Slot definitions can be a simple identifier or a list:

  (SlotName :initform InitValue :initarg :InitArgname :allocation :class)
  • SlotName: Name of member or method. This is required. All other are optional.
  • :initform InitValue: On creation of an object of this class the slot will be initialized with this value.
  • :initarg :InitArgname: Named argument in make-instance will be used to initialize this slot at creation. Note: a slot will first be initialized with :initform and then with the argument given with make-instance.
    Slots without :initform or :initarg will be Nil.
  • :allocation :class: The slot is 'static'

 Create a Class in Lisp

make-instance creates an object instance of a previosly defined class (using defclass).

(make-instance 'ClassName &rest named-args)
  • make-instance: build in method to create an object instance
  • 'ClassName: name of the Class previosly defined with defclass
  • &rest named-args: a list of named args. The named args must match To one of the slot difinition of the class or its superclasses.

 Accessing Slots

The solution for accessing slots is different CLOS.
CLOS defines the function slot-value and the :accessor attribute for slot defintions. These methods are not used in ACDK lisps.
To reflect the syntax of common languages like C++, Java, Perl, Python, etc.
the object instance is directly callable:

(defclass AClass ()
  ( 
    (foo :initform 2) 
    (bar :initarg :bar)
  )
)
(setf lobj (make-instance 'AClass :bar 4))
(lobj 'foo) ;; reads slot -> 2
(lobj 'bar) ;; reads slot -> 4
(lobj 'bar 6) ;; sets slot -> 6

 Object Methods

The keyword defmethod from CLOS is not supported in ACDK Lisp.
For a better interaction with other ACDK DMI components the object system is designed to be compatible with ACDK Object system.

To implement member methods for a class just set a slotvalue to a function.


(defun AClass-foo-method (self a b)
  (+ a b (self 'number))
)
(defclass AClass ()
  (
    (number :initarg :number)
    ; set foo to external defined function
    (foo :initform AClass-foo-method) 
    ; use lamba to define an anon function
    (bar :initform (lambda (self a b) 
                          (+ a b (self 'number))))
  )
)
(setf lobj (make-instance 'AClass :number 3))
; calling foo method
(lobj 'foo 3 4) ; -> 10
; calling bar method
(lobj 'bar 3 4) ; -> 10