From: Joris Bleys
Subject: accessing print-object of superclass
Date: 
Message-ID: <pan.2004.04.14.12.28.01.228055@nomasp.vub.ac.be>
Hello everyone who reads this,

I'm having troubles finding a way to access the print-object function of
my superclass. As LISP uses generic functions to select the function which
is called, I suppose I have to cast it to the superclass.

As a second question: I have serious doubts about the portability of the
with-slots macro to other LISP-implementations. Are these doubts justified
and if so, how should it be rewritten in ANSI Lisp ? ;)

For instance:

(defclass super-class ()
  ((super-class-slot :initform 0))
  ())

(defclass sub-class (super-class ()
  ((sub-class-slot :initform 1))
  ())

(defmethod print-object ((instance super-class) stream)
  (print-unreadable-object (instance stream)
    (with-slots (super-class-slot) instance
      (format stream "Super-class-slot: ~D" super-class-slot))))

(defmethod print-object ((instance sub-class) stream)
  (print-unreadable-object (instance stream)
    (with-slots (sub-class-slot) instance
      (format stream "Sub-class-slot: ~D" sub-class-slot)
      ;;Insert call to print-object of super-class here
)))

Any help would be greatly appreciated,

Joris 

From: Pascal Costanza
Subject: Re: accessing print-object of superclass
Date: 
Message-ID: <c5jbif$119g$1@f1node01.rhrz.uni-bonn.de>
Joris Bleys wrote:
> Hello everyone who reads this,
> 
> I'm having troubles finding a way to access the print-object function of
> my superclass. As LISP uses generic functions to select the function which
> is called, I suppose I have to cast it to the superclass.

No. call-next-method is your friend.

  > As a second question: I have serious doubts about the portability of the
> with-slots macro to other LISP-implementations. Are these doubts justified
> and if so, how should it be rewritten in ANSI Lisp ? ;)

Again, no. with-slots is part of ANSI Common Lisp, so go ahead and use 
it. There was a recent discussion here about the use of with-slots in 
conjunction with structures. This is indeed not sanctioned by the 
standard. But it's perfectly ok to use them with classes.

You can easily determine whether something is part of ANSI by using the 
HyperSpec.

> (defmethod print-object ((instance sub-class) stream)
>   (print-unreadable-object (instance stream)
>     (with-slots (sub-class-slot) instance
>       (format stream "Sub-class-slot: ~D" sub-class-slot)
>       ;;Insert call to print-object of super-class here
> )))

That would be:

(defmethod print-object ((instance sub-class) stream)
   (print-unreadable-object (instance stream)
     (with-slots (sub-class-slot) instance
       (format stream "Sub-class-slot: ~D" sub-class-slot)
       (call-next-method))))


Pascal

-- 
ECOOP 2004 Workshops - Oslo, Norway
*1st European Lisp and Scheme Workshop, June 13*
http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/
*2nd Post-Java Workshop, June 14*
http://prog.vub.ac.be/~wdmeuter/PostJava04/
From: Joris Bleys
Subject: Re: accessing print-object of superclass
Date: 
Message-ID: <pan.2004.04.14.12.53.24.255127@nomasp.vub.ac.be>
Ok, thanks a lot Mr. Costanza !!

Joris
From: Peter Seibel
Subject: Re: accessing print-object of superclass
Date: 
Message-ID: <m3n05em4sq.fsf@javamonkey.com>
Pascal Costanza <········@web.de> writes:

> Again, no. with-slots is part of ANSI Common Lisp, so go ahead and
> use it. There was a recent discussion here about the use of
> with-slots in conjunction with structures. This is indeed not
> sanctioned by the standard. But it's perfectly ok to use them with
> classes.

A note to the OP and other new Lisp lurkers: while I'm sure Pascal is
aware of the nit I'm about to pick, and was simply speaking
informally, it is not strictly correct to contrast "structures" to
"classes" as in Common Lisp the data structures defined with DEFSTRUCT
and DEFCLASS are *both* classes, just different kinds of classes
(strictly speaking: they have different meta classes.) Which you
needn't worry about for now--which is probably why Pascal didn't
bother making the distinction. But keep it in the back of your
mind--someday it will help you understand some things (like why you
can define methods that specialize on structures as well as DEFCLASS'd
classes. Not to mention built in classes such as NUMBER and CONS.)

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp