Couldn't just define <square> like this:

(define-class <square> (<rectangle>))

then define a side method:

(define-method (side (@ <square>)) (slot-ref @ 'height))

This way, you're not storing the same information twice.


On 22 July 2014 14:18, Marko Rauhamaa <ma...@pacujo.net> wrote:

>
> Consider this simple program:
>
> ========================================================================
> (use-modules
>  (oop goops)
>  (ice-9 optargs))
>
> (define-class <rectangle> ()
>   (width #:accessor width #:init-keyword #:width)
>   (height #:accessor height #:init-keyword #:height))
>
> (define-method (area (@ <rectangle>))
>   (* (height @) (width @)))
>
> (define-class <square> (<rectangle>)
>   (side #:accessor side #:init-keyword #:side))
>
> (define-method (initialize (@ <square>) args)
>   (let-keywords
>    args #f ((side #f))
>    (next-method @ (list #:width side #:height side))))
>
> (format #t "~S\n" (area (make <square> #:side 3)))
> (format #t "~S\n" (side (make <square> #:side 3)))
> ========================================================================
>
> The program outputs:
>
> ========================================================================
> 9
> ERROR: Unbound slot in object #<<square> b76cfec0>
> ========================================================================
>
> I understand that by overriding <square>'s initialize method I'm losing
> the magic of the default initializer. How could I have the cake and eat
> it, too?
>
>
> Marko
>
>

Reply via email to