An Object in Lisp. Part 6

What a way to start a little update on the the object framework that I have been pushing on.

The last update talked about ‘destructuring-bind‘. I had an epipheny the other day when I was trying to tdd a way to create generic functions for the framework. That led me to throwing away the destructuring-bind.

In order to explain my reasoning we need to go back to the representation of an object in the framework. The representation is a list of closures referenced by properties.

(defun hello (obj &rest args)
  (apply (getf obj :hello) args)

(defvar hold1 (list :hello (lambda () (print 'hello))))
(defvar hold2 (list :hello (lambda (x) (print x))))

The above lists can be accessed with a getf that retrieves the lambda from the list. To execute the lambda just apply. The hello defun above will handle either list as obj.

Something that I now know,  is that the lambda does its own destructuring-bind on its parameter list.  So here is how I create the accessor function for all objects  (generic and multiple signatures):

(defmacro make-property (name)
  (let ((g (gensym)))
    (multiple-value-bind (sym-name key-name) (symbol-and-keyword name)
      `(progn
         (if (not (fboundp ',sym-name))
             (defun ,sym-name (obj &rest ,g)
               (block ,sym-name
                 (apply (getf obj ,key-name) ,g)))))))

Happy New Year!

gutzofter

About these ads

One Response to An Object in Lisp. Part 6

  1. [...] An Object in Lisp. Part 6 « (defun ugly-lisp-code? () ()) [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: