cl-def

Annoyed by define-foo-bar and friends?

Introduction

cl-def is another def macro (not unlike definer) that lets you use a standard syntax for defining stuff. The important difference is on cl-def's focus on letting the user extend its functionality, like the special flags for function definitions. Some examples follow:

Fix the deficiencies of cl:defconstant:

(def (constant :test 'equalp) +serializers+ (make-array 128))
(progn
 (defconstant +serializers+
   (let ((new (make-array 128)))
     (if (boundp '+serializers+)
         (let ((old (symbol-value '+serializers+)))
           (cond
            ((constantp '+serializers+)
             (cond ((equalp old new) old)
                   (t
                    (cerror "Try to redefine the constant." "~@<~S is an already defined constant whose value ~
                               ~S is not equal to the provided initial value ~S ~
                               under ~S.~:@>"
                            '+serializers+ old new 'equalp)
                    new)))
            (t
             (cerror "Try to redefine the variable as a constant." "~@<~S is an already bound non-constant variable ~
                            whose value is ~S.~:@>"
                     '+serializers+ old)
             new)))
         new))))

Add some nice local flags for defun (inline, optimize, export):

(def (function ioe) invalidate-cached-slot (instance slot)
  "Invalidates the given cached slot value in the instance."
  (setf (standard-instance-access instance (slot-definition-location slot)) +not-cached-slot-value+))

which looks up and calls an optional function in *package* to further refine the definer flags before the expansion:

(defun transform-function-definer-options (options)
  (if cl-perec-system:*load-with-debug-p*
      (remove-keywords options :inline :optimize)
      options))

and expands to this when loaded without the debug flag:

(progn
 (declaim (inline invalidate-cached-slot))
 (export 'invalidate-cached-slot)
 (defun invalidate-cached-slot (instance slot)
   "Invalidates the given cached slot value in the instance."
   (declare (optimize (speed 3) (debug 0)))
   (setf (standard-instance-access instance (slot-definition-location slot)) +not-cached-slot-value+)))

A common special-variable definer for defvar/defparameter that doesn't make it unnecessarily hard to define an unbound special variable with documentation:

(def (special-variable e :documentation "The t parameter known from physics.") *t*)
(progn
  (export '*t*)
  (progn
    (setf (documentation '*t* 'common-lisp:variable) "The t parameter known from physics.")
    (defvar *t*)))

You can find many other examples for cl-def usage in cl-serializer.

Mailing Lists

Project members

Attila Lendvai
Levente Mészáros

Repo

You can browse the cl-def darcs repository or get the tree with

darcs get --partial http://common-lisp.net/project/cl-def/darcs/cl-def

License

BSD / Public Domain