Re: Where to file one defgeneric for two classes and two packages
- From: Paul Donnelly <paul-donnelly@xxxxxxxxxxxxx>
- Date: Tue, 18 Mar 2008 22:21:19 GMT
Mirko.Vukovic@xxxxxxxxx writes:
OK, but how would one then handle the following contrived case:
Suppose I want to draw pictures and lines on a display. For pictures
I just apply the draw method, but for lines, I may want to specify the
line thickness (as I said, the example is contrived).
It seems that I am thus forced to specify draw-picture and draw-line
methods, instead of just draw for each of them.
Presumably your picture and line objects keep track of their own
geometry and are drawn with (draw picture) and (draw line),
respectively. There's no point in having a class if that class doesn't
store any data. I'm just sidestepping your example, of course, but I'm
having trouble understanding why it would be helpful to use a generic
function if it were necessary to call it differently depending on the
type of the object. That doesn't seem very generic to me. Consider:
(defclass picture (graphic)
...)
(defclass line (graphic)
...)
(defgeneric draw (graphic)
(:method ((picture picture)) ...)
(:method ((line line)) ...))
(defparameter *list-of-graphics* (list (make-instance 'picture)
(make-instance 'line)
(make-instance 'line)
(make-instance 'picture)
...))
(dolist (g *list-of-graphics*)
(draw g))
This is the kind of situation I envision calling a generic function
in. I don't know the type of the object I'm drawing (I could check at
run-time, but that's what generic functions do for me) and don't need to
worry about it.
The issue is not a show-stopper for me. I was just surprised that
just providing the object class and specifying (or not) an argument is
not enough data for the compiler to figure out which method to apply.
I don't think it would be a problem for the Lisp system. I think the
reasoning is that it would be pointless. If you're inspecting the type
of your object and collating it with data from some other source,
there's no point in automatic dispatch based on type... you've already
done what the generic function would do and might as well type a few
more letters in the function name.
.
- Follow-Ups:
- Re: Where to file one defgeneric for two classes and two packages
- From: Mirko . Vukovic
- Re: Where to file one defgeneric for two classes and two packages
- References:
- Where to file one defgeneric for two classes and two packages
- From: Mirko . Vukovic
- Re: Where to file one defgeneric for two classes and two packages
- From: Thomas A. Russ
- Re: Where to file one defgeneric for two classes and two packages
- From: Mirko . Vukovic
- Where to file one defgeneric for two classes and two packages
- Prev by Date: ELS'08 News!
- Next by Date: Re: Making a function "forget" a variable
- Previous by thread: Re: Where to file one defgeneric for two classes and two packages
- Next by thread: Re: Where to file one defgeneric for two classes and two packages
- Index(es):
Relevant Pages
|