guile-user
[Top][All Lists]
Advanced

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

Re: GOOPS constructors


From: Tobias Brandt
Subject: Re: GOOPS constructors
Date: Tue, 22 Jul 2014 14:22:31 +0200

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 <address@hidden> 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

[Prev in Thread] Current Thread [Next in Thread]