Open In App

Common LISP Object System

Last Updated : 22 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

CLOS or Common LISP Object System is one of the most powerful object systems available. It is dynamic, reliant, and supports multiple inheritance and multiple dispatches. It is different from most object systems as it has classes and functions(methods) that are not tied together. Also, it is provided by a protocol known as a meta-object protocol, which offers a standard interface for the CLOS, and can be used to create new object systems

Defining Classes:

For defining classes in Lisp we use defclass macro. 

Syntax:

  (defclass <class-name> (list of super classes)
 ((slot-1
 :slot-option slot-argument)
  (slot-2, etc))
 (:optional-class-option
  :another-optional-class-option))

(defclass Name ()
(First_Name
Middle_Name
Last_Name)
)
; This code makes a class 'Name' with 3 slots

The slots are the variables that stores data and fields in them. The slots come with multiple slot options comprising of keywords and name, expression, and other options which are:

  • :accessor function-name
  • :initform expression
  • :initarg symbol

Creating Instances of Class: 

Instances could be easily made using make-instance.

Syntax:

make-instance class {initarg value}*)

Example 1:

Lisp




; Defining a class Names with 
; Slots First_Name and Last_Name
(defclass Names ()
   ((First_Name :accessor name_1)
      (Last_Name :accessor name_2)
      
   )
)
; Creating a instance
(setf Person (make-instance 'Names))
  
; Setting the name for the instance
(setf (name_1 Person) "Kishan")
(setf (name_2 Person) "Pandey")
  
; Printing the name
(format t "The First Name of the 
        person is ~d~%" (name_1 Person))
(format t "The Last Name of the 
        person is ~d~%" (name_2 Person))


Output:

 

Providing Access and Read/Write Control to a Slot:

For making a functional class we have functionalities to access, read and write into the slot. Specifying accessors for each slot while defining the class:

(defclass Names ()
   ((First_Name :accessor name_1)
      (Last_Name :accessor name_2)
   )
)

Note: You can also specify separate accessor names for reading and writing a slot

Lisp




(defclass Names ()
  ((First_Name :reader get-First_Name :writer set-First_Name)
      (Last_Name :reader get-Last_Name :writer set-Last_Name)
      
  )
)


Inheritance :

Inheritance is a method to define one object(child-object) in terms of another(parent-object). In Lisp, we can define child classes that inherit the properties of the parent class.

Example 2:

Lisp




(defclass BMI ()
   (
      (weight :accessor person-weight)
      (height :accessor person-height)
      (BMIcalc :reader BMI-calc)
   )
)
  
; method calculating BMI   
(defmethod BMIcalc ((object BMI))
   (/(person-weight object)(*(person-height object)(person-height object)))
)
    
;child_BMI inherits BMI class  
(defclass child-BMI (BMI)
((health :accessor person-health)))
  
;setting the values 
(setf item (make-instance 'child-BMI))
(setf (person-weight item) 70)
(setf (person-height item) 1.8)
(setf (person-health item) "Good")
  
; displaying the values
(format t "BMI is: ~d~%" (BMIcalc item))
(format t "Health is: ~d~%" (person-health item))


Output:

 

Defining the Method of a Class:

We will use the defmethod macro to define a method inside the class as follows:

Example 3:

Lisp




(defclass BMI ()
   (
      (weight :accessor person-weight)
      (height :accessor person-height)
      (BMIcalc :reader BMI-calc)
   )
)
  
; Defining a method of class that calculates the BMI
(defmethod BMIcalc ((object BMI))
   (/(person-weight object)(*(person-height 
                              object)(person-height object)))
)
    
;setting the values 
(setf item (make-instance 'BMI))
(setf (person-weight item) 70)
(setf (person-height item) 1.8)
  
  
; displaying values
(format t "BMI is: ~d~%" (BMIcalc item))


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads