Answers from 2017 Common Lisp experts – (blog ‘phoe) – phoe’s Blog – Teknik

https://blog.teknik.io/phoe/p/365

This might be useful for an up to date overview for people starting with Lisp.

Advertisements
Link

Thoughts on creating objects in Lisp

If you work with classes in Lisp, you might want to hide the details of creating an object of a class. This practise is generally known as the factory method pattern. I’ll show a very simple example.

Let’s assume, you have defined the following class.

(defclass noun ()
  ((es
    :accessor noun-spanish)
   (es-gender
    :accessor noun-spanish-gender)
   (de
    :accessor noun-german)
   (de-gender
    :accessor noun-german-gender)))

To create an object of this class, you have to call make-instance and setf the attributes (slots). To avoid writing this again and again, you can define the following function.

(defun create-noun (es es-gender de de-gender)
  (let ((n (make-instance 'noun)))
    (progn (setf (noun-spanish n) es)
       (setf (noun-spanish-gender n) es-gender)
       (setf (noun-german n) de)
       (setf (noun-german-gender n) de-gender)
       n)))

You can then use this function to create objects of your class.

CL-USER> (defparameter *noun* (create-noun 'falda 'f 'Rock 'm))
*NOUN*
CL-USER> (noun-german *noun*)
ROCK
CL-USER> (noun-spanish *noun*)
FALDA
CL-USER>

While this is a very simple application of a factory method (or function), you might still find it useful.

Thoughts on creating objects in Lisp

Defining Lisp classes in packages

In Lisp, when you define classes in packages, you have to remember that you have to explicitly export the accessors too. For example, you define the following package with a class inside.

(defpackage :mhn-foo
  (:use :common-lisp)
  (:export #:fooclass))
(in-package :mhn-foo)
(defclass fooclass ()
  ((fooatt
    :accessor fooattribute)))

However, while you can instantiate the class, you get an error message when you try to access the attribute of the class.

CL-USER> (defparameter *fooparameter* (make-instance 'mhn-foo:fooclass))
*FOOPARAMETER*
CL-USER> (setf (mhn-foo:fooattribute *fooparameter*) "foo")
; Evaluation aborted on #<SB-INT:SIMPLE-READER-PACKAGE-ERROR "The symbol ~S is not external in the ~A package." {1005FDFC03}>.

To be able to access the attribute of the class, you have to export it.

(defpackage :mhn-foo
  (:use :common-lisp)
  (:export #:fooclass #:fooattribute))

Now you can work with the attribute of the class without problems.

CL-USER> (defparameter *fooparameter* (make-instance 'mhn-foo:fooclass))
*FOOPARAMETER*
CL-USER> (setf (mhn-foo:fooattribute *fooparameter*) "foo")
"foo"
CL-USER> (mhn-foo:fooattribute *fooparameter*)
"foo"

This is not very different in other programming languages. For example, in Java and C#, you define visibility not only for the class, but for the class members too. If you forget to set visibility for class members, you might have problems accessing them.

Defining Lisp classes in packages