about genricity

From: Andre Gilles <andre_at_crs.etca.fr>
Date: Wed, 11 Sep 1996 11:11:38 +0200

Hello,
        
        I am writing a software based on classes that write and read themselves
on files. Here is the kind of class hierarchy I use :

(define-class parent-class ()
...

(define-class child-class-1 (parent-class)
...

(define-class child-class-2 (parent-class)
...

then when I want to write the class I introduce genric functions
such as :

(define-method write ((c child-class-1) port)
...

(define-method write ((c child-class-1) port)
...

        And now I can write polymorphic code for example :

(map (lambda (c) (write c port))
     list-of-class)

        I'm quite happy with that.

        When I want to read a class, I don't know how to do it in a clean way.
Here is what I do

        (while not-eof-port
               read line on port
               if line match a regexp corresponding to child-class-1
                        the create an instance of child-class-1
                        (read child-class-1 port)
               if line match a regexp corresponding to child-class-2
                        the create an instance of child-class-2
                        (read child-class-2 port)
                ...

        Each time I create another heir of parent-class I have to modify this
code. I find that this sequence of "if" is against the spirit of
polymorphism but I can't manage to find another way to do it. Is there a
classic way to handle such problems ?

        Gilles
Received on Wed Sep 11 1996 - 10:12:58 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST