Following on from my horticulturally-challenged CLOS questions, could
any anybody help me with this. I would like to see if something is a
potato. So, as before:
(defclass potato
(dimensions :initarg dimensions
:type vector
:initform #(0)))
Then create a potato and a non-potato:
(setf *small-potato* (make-instance 'potato :initarg #(1 2 3)))
(setf *not-a-potato* 42)
I can test for potato'ness either by:
(defmethod potato-p-1 ((p potato))
t)
(defmethod potato-p-1 ((p t))
nil)
or by:
(defun potato-p2 (p)
(typep p 'potato))
Which is the best test of my *small-potato* or *not-a-pototo* for true
potato'arity? Thank you any help you can give me.
:) will
"and my vegetable love shall grow, vaster than empires
and more slow" -- A.Marvell
William Deakin <·····@pindar.com> writes:
> Following on from my horticulturally-challenged CLOS questions, could
> any anybody help me with this. I would like to see if something is a
> potato. So, as before:
>
> (defclass potato
Missing a () here. Assuming it's just a typo.
> (dimensions :initarg dimensions
> :type vector
> :initform #(0)))
Missing a set of parens around (dimensions ...), too. The entire
set of slots is the third arg to defclass, not a rest argument.
> Then create a potato and a non-potato:
>
> (setf *small-potato* (make-instance 'potato :initarg #(1 2 3)))
For the definition you've given, you want
(make-instance 'potato 'dimensions ...)
The :initarg in the defclass identifies what argument you will use
in the make-instance. You have specified dimensions.
> (setf *not-a-potato* 42)
>
> I can test for potato'ness either by:
>
> (defmethod potato-p-1 ((p potato))
> t)
>
> (defmethod potato-p-1 ((p t))
> nil)
I recommend the above.
> or by:
>
> (defun potato-p2 (p)
> (typep p 'potato))
I bet fewer implementations will make this efficient. In some
implementations, TYPEP of a declared class is aggressively optimized
at compile time, but some do not (in order to allow you to do
redefinition cleanly).
> Which is the best test of my *small-potato* or *not-a-pototo* for true
> potato'arity? Thank you any help you can give me.
Since TYPEP *potentially* does a lot of work (checking for DEFTYPE's,
doing runtime calls FIND-CLASS, etc) before discovering that you want
to check for class equality, and since the generic dispatch mechanism
you offer bypasses all that, I recommend using the generic dispatch
mechanism.
> :) will
>
> "and my vegetable love shall grow, vaster than empires
> and more slow" -- A.Marvell
Hoe, hoe, hoe!
Happy gardening.
Thanks for your help. I'm sorry about the mistake but I posted the mail from
a different machine than that with which I was working. So that the code was
typed in (rather badly) from memory.
Best Regards,
:) will