[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CLOS accessors & declarations



>Date: Wed, 28 Oct 1992 14:00:28 -0500
>From: jbk@world.std.com (Jeffrey B Kane)
>To: info-mcl@cambridge.apple.com
>Subject: CLOS accessors & declarations
>
>
>A quick question that has me a bit perplexed.  In CLOS you let 
>the evaluator know what type of object you are dealing with by
>using methods, i.e.
>
>(defclass TObj ()
>  ((fSlot :accessor my-accessor)))
>
>(defmethod Do-Something ((anObj TObj))
>  (my-accessor anObj))
>
>this lets the evaluator know that my-accessor should be tied to
>anObj of type TObj.
>
>The problem I am having is when I have a list of objects, I
>can't figure out how to tell the evaluator that this list only
>contains objects of type TObj, for example:
>
>(doloop (anObj list-of-TObj)
>        (print (my-accessor anObj)))
>
>Adding a simple declaration to the loop does not seem to work:
>
>(doloop (anObj list-of-TObj)
>        (declare (type TObj anObj))
>        (print (my-accessor anObj)))
>
>Any idea of the proper way to do this so the evaluator doesn't
>yip at me when I evaluate the above construct?
>
>       Jeffrey


I think you mean dolist, not doloop.

I think you're unnecessarily worried here. If list-of-Tobj
only contains objects of type Tobj, then you're all set.
This code will work just fine with no declarations, because
in lisp, everything already knows what type it is.

(dolist (anObj list-of-TObj)
   (print (my-accessor anObj))

If you want to program defensively (not a bad strategy if you
enjoy robust code), you can always throw in something like this 
before the DOLIST to make sure the list is kosher:

(assert (every #'(lambda (x) (typep x 'Tobj)) 
               list-ofTobj))