CLASS

STRUCTURE


                         Use the proper macro to define the type you  wish to have 
                               
     (defclass schoolkid ( )
            ( (name )
              (age :initform 16)
              (school)  )
      )
          
     (defstruct schoolkid
           (name)
           (age 16)
           (school)
     )
          
 
                             To create an instance of the class or structure use the
                                   correct "make" function.
                             This will make an actual object of that type.
    
                           

The template to create an actual instance (or
object) of a class is:
  (setf <object-name> (make-instance <class name> 

Example:
  >(setf johnny(make-instance 'schoolkid) )
  #<SCHOOLKID #x20277EF9>
 
This creates an object (or instance) with empty
slots, except where initform was used.
          
In the case of a struct use 
       make-structname

 (setf  jonni(make-schoolkid))
#S(SCHOOLKID :NAME NIL :AGE 16 :SCHOOL NIL)

This also creates an object with empty 
s
lots , except where the initial value was
put.
            
    The macro  defclass  has slot definitions that defstruct does 
    not have - slot definitions are lists composed of the slot
    name followed optionally by pairs of the following slot option
    keywords and associated values.
    
    Values supplied by :initform, :allocation, and :documentation
    override any previously supplied value in a superclass
    
  • :initarg this is used in the make-instance to set keywords for slot definitions
  • :accessor the name of a function to read, and (with setf) set the slot
  • :initform a default value for the slot (unbound otherwise)
  • :allocation whether slot is shared or specific to each instance
  • :type a Common Lisp type for use in compiler optimizations

(defclass schoolkid ( )
   ((name   :initarg :name   :accessor kid-name)
    (age    :initarg :age    :accessor kid-age 
                             :initform 16)
    (school :initarg :school :accessor kid-school
                             :type string)
    )
)
       
              
                  No equivalent

             
 
                       The following will fill the slots.

           
Method 1: 
Using initarg
(setf juan(make-instance 'schoolkid
                        :name "Juan Peron")) 
#<SCHOOLKID #x20288929>

Method 2:                       
Using generic slot functions
(setf (slot-value johnny'name)"John Paul II")
"John Paul II"

Method 3: 
Using the accessor function
(setf (kid-school juan'school)"Kiddie Garter")
"Kiddie Garter"
            
  Method 1: 
At the creation of the instance:
(setf juanita(make-schoolkid
                 :age 14))
#S(SCHOOLKID :NAME NIL :AGE 14 :schooL NIL"


Method 2: 
With the setf
(setf (schoolkid-age jonni)19 
16

(setf (schoolkid-school juanita) "TJ")
"TJ"

            
                     Now to write out the information.  First the  slots individually.
            
Method 1: 
Using the accessor

(kid-name johnny)
"John Paul II"
(kid-school johnny)
*** - EVAL: variable SCHOOL has no value     
(kid-school juan)
"Kiddie Garter"

Method 2:
Using the generic slot names

(slot-value juan' name)
"Juan Peron"
(slot-value juan' age)
16
(slot-value juan' school)
"kiddie Garter"
            
(schoolkid-name juanita)
NIL
(schoolkid-age juanita)
14
(schoolkid-age jonni)
19            
           
                     Finally, to dump all the information using describe. 
           
 (describe johnny)
#<SCHOOLKID #x20277EF9> is an instance of the CLOS class #1=#< STANDARD-CLASS
 SCHOOLKID>.
Slots:
    NAME    =  "John Paul II"
    AGE     = 16 
    SCHOOL  unbound

  "John Paul II" is a simple 1 dimensional array (vector)
   of characters, of size 12.


16 is an integer, uses 5 bits, is represented as
 a fixnum.

         
(describe juanita)
#S(SCHOOLKID :NAME NIL :AGE 11
  :SCHOOL "TJ")
This is a structure of type SCHOOLKID.

          
There are several predicate functions that can check on the existence of the object and, in the case of classes, even if the slot itself exists.

The class has a predicate <class-name>-p 
which will check to see if a  given object is
of that type
       (schoolkid-p johnny) ==> T
However
       (schoolkid-p elvis) ==>Nil 
    if elvis is some other type of object.

Also, the more generic typep
       (typep juan 'schoolkid) --> T

There also functions slot-exists-p
and slot-boundp for checking  if a given
instance has a certain slot or if that
slot has a value.

     (slot-exists-p juan 'grade) ==> NIL 

     (slot-bound-p johnny 'school) -->NIL


           
  
For structures only their existence is
detectable using:
The generic
   (typep juanita schoolkid) --> T

and the specific
   (schoolkid-p jonni)  --> T