From: Hallvard Tr{tteberg
Subject: using methods and error
Date: 
Message-ID: <HALTRAET.92Jun30141900@sparc.si.no>
Hi, I'm using CLOS a lot and need some advise concerning style.

Suppose you want a method foo (of course) like this:

(defmethod foo ((self a-class) item)
  (pushnew item (an-accessor self)))

(Its important to discriminate on the the first parameter because other
classes might want to do other things.)

However, I want to do some error checking, since I know that item
should always be of type bar. There are three possibilities:
1. Add a declaration, hoping that the implementation inserts a type
   check.
2. Qualify the second parameter and rely on the
   no-applicable-method error.
3. Insert an error check of my own, e.g. using error, check-type or
   assert.

The second possibility is easiest but may give the reader of the code
the wrong impression, since I never intend to qualify on anything
else. Adding a declaration gives me less control over what happens
when passing the wrong type. That leaves me with the third
alternative, but I thought I'd ask other Lispers for a comment on this
choice.

Regards, Hallvard
--
Hallvard Traetteberg
Dept. of Knowledge Based Systems
Center for Industrial Research
Box 124 Blindern, 0314 Oslo 3
NORWAY

Tlf: +47 2 45 20 10
Fax: +47 2 45 20 40
Email: ········@si.no

From: Barry Margolin
Subject: Re: using methods and error
Date: 
Message-ID: <12q56gINN79u@early-bird.think.com>
In article <······················@sparc.si.no> ········@sparc.si.no (Hallvard Tr{tteberg) writes:
>(defmethod foo ((self a-class) item)
>  (pushnew item (an-accessor self)))

>However, I want to do some error checking, since I know that item
>should always be of type bar. There are three possibilities:
>1. Add a declaration, hoping that the implementation inserts a type
>   check.
>2. Qualify the second parameter and rely on the
>   no-applicable-method error.
>3. Insert an error check of my own, e.g. using error, check-type or
>   assert.

1 is probably not a good solution in this case.  Most implementations use
declarations for optimization purposes, not for error checking (some
implementations do optimization in the compiler and error checking in the
interpreter, and CMU CL is the only implementation I can think of that
normally turns declarations into error checks).

I like 3, and I suggest you use CHECK-TYPE.  It gives a reasonable error
message and restart option.

2 isn't bad, either, although as you said, it implies that methods for
other classes of second arguments should be defined.  The
no-applicable-method error message may not be as clear to the user as
check-type's message, and it probably won't provide a restart that lets the
user supply a replacement value.

Note that leaving the second argument unqualified doesn't *prevent* someone
from defining a method that qualifies it.  An unqualified parameter is just
an abbreviation for (item t).

Another factor against qualifying the second parameter could be
performance.  I'm not really familiar with the internals of any CLOS
system's dispatch mechanisms, but I expect that it's easier to generate
good code for single dispatch than multiple dispatch.  Therefore,
implementations may notice that all the methods of a generic function
dispatch on the same one argument, and use a more efficient dispatching
technique.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar
From: Clinton Hyde
Subject: Re: using methods and error
Date: 
Message-ID: <CHYDE.92Jul6170921@pecos.ads.com>
i'm not completely sure i understood what you are uncertain of, but
here's a whack at it:

do two methods:

(defmethod foo ((self a-class) item)	; 'item' will be of type 'T'
  ;;do error checking here, with your own method.
  ;;thus avoiding the no-applicable-method condition.
  ;;flag specific non-BAR 'items' how you wish.
  )

(defmethod foo ((self a-class) (item bar))
  ;;do good things with BARs.
  (pushnew item (an-accessor self)))

this probably covers yr needs.

;;;1. Add a declaration, hoping that the implementation inserts a type
;;;   check.
;;;2. Qualify the second parameter and rely on the
;;;   no-applicable-method error.
;;;3. Insert an error check of my own, e.g. using error, check-type or
;;;   assert.

well, re: 

1) it does. method dispatching checks types. that's why the above
	example works the right way.
2) not necessary, use above example.
3) also probably not necessary, but in the first method (where item is
	type 'T'), you might do a TYPECASE if you had to distinguish.

 -- clint
--

Clint Hyde		"Give me a LispM or give me death!" -- anonymous

Advanced Decision Systems	Internet:  ·····@chesapeake.ads.com
2111 Wilson Blvd #800
Arlington, VA 22201		(703) 875-0327