For some reason I'm reluctant to write macros to
automatize the declaration of classes. It might
sound like a silly idea to begin with, because
aren't classes and subclasses supposed to automatize
such things? They are, but to me it looks like
the most economical solution. In my particular
case, this even works. In lesser languages like
Lisp one could not do this at all. So: Does it
suck to refactor a class-declaration pattern
into a macro or is it perfectly acceptable in Lisp?
Have you ever done this? If so, would you admit
it in public?
--
Lisp kann nicht kratzen, denn Lisp ist fluessig
Tin Gherdanarra wrote:
> So: Does it
> suck to refactor a class-declaration pattern
> into a macro or is it perfectly acceptable in Lisp?
After all, defclass is a macro, too, why not wrapping it with a macro,
which does what you need? E.g. something like the code below, which defines
a class, which may be used for classes for storing data, which allows
Visual Basic and Pascal like "with"-constructs.
(defmacro make-db-class (name fields)
`(progn (defclass ,name ()
,(loop for field in fields collect
`(,field :initform ()
:initarg ,(intern (symbol-name field) "KEYWORD")
:accessor ,field)))
(defmacro ,(intern (concatenate 'string
"WITH-ALL-" (symbol-name name)
"-SLOTS"))
(object &body body)
`(with-slots ,',fields ,object ,@body))))
(make-db-class contact (first-name last-name))
(defun test ()
(let ((foo (make-instance 'contact)))
(with-all-contact-slots foo
(setf first-name "Frank"
last-name "Buss")
(format t "~a ~a" first-name last-name))))
--
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Tin Gherdanarra wrote:
> So: Does it
> suck to refactor a class-declaration pattern
> into a macro or is it perfectly acceptable in Lisp?
> Have you ever done this? If so, would you admit
> it in public?
>
>
Wisdom Systems (1986-1993, R.I.P.) marketed an expert system shell that
used at its core such macros to define a metalanguage for part
definition. Such chicanery enabled constructs like:
(define-part foo (inherit-from-list)
:properties (a 3
b 4
c (+ (the a) (the b))
:subparts ((foo-box :class 'box
width (the a) ;these are properties on class box
depth (the b)
height (the c))))
where instantiation of the part foo caused creation of the object
network necessary to maintain values - if you did
(change-value (the foo a) 42)
then all appropriate properties would be marked for recomputation when
some user action forced their evaluation. It is difficult to convey the
usefulness of these constructs in a reasonably short response, but you
have my word that many people found this a powerful system for the
precise reason that it used such macros.
Raymond Laning
From: Thomas A. Russ
Subject: Re: Style question: DEFCLASS/DEFSTRUCT in macros?
Date:
Message-ID: <ymiodxkifea.fsf@sevak.isi.edu>
Tin Gherdanarra <···········@gmail.com> writes:
> For some reason I'm reluctant to write macros to
> automatize the declaration of classes.
No need to be.
> So: Does it
> suck to refactor a class-declaration pattern
> into a macro or is it perfectly acceptable in Lisp?
> Have you ever done this? If so, would you admit
> it in public?
We do it a lot. In fact, one often ends up writing a macro that expands
(via PROGN) into not just the class definition, but also some method
definitions as well.
--
Thomas A. Russ, USC/Information Sciences Institute
In article <···············@individual.net>,
Tin Gherdanarra <···········@gmail.com> wrote:
> For some reason I'm reluctant to write macros to
> automatize the declaration of classes. It might
> sound like a silly idea to begin with, because
> aren't classes and subclasses supposed to automatize
> such things? They are, but to me it looks like
> the most economical solution. In my particular
> case, this even works. In lesser languages like
> Lisp one could not do this at all. So: Does it
> suck to refactor a class-declaration pattern
> into a macro or is it perfectly acceptable in Lisp?
> Have you ever done this? If so, would you admit
> it in public?
Any time you find yourself repeatedly typing the same syntax with minor
variations in the same place, it makes sense to refactor it into a
macro. There's nothing special about DEFCLASS and DEFSTRUCT that
suggest that they should not appear in the expansion of macros.
I'm not sure what you mean by "lesser languages like Lisp". Lisp is not
a single language, it's an entire family of languages. But I'm not
aware of any Lisp dialects that have macros but have limitations on what
they can expand into. If they have macros the expanded expression is
typically processed as if it had been an original expression, which
means that it may contain uses of macros which will then be expanded
(e.g. DEFCLASS).
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***