* Sharing inheritance ? @ 1998-11-02 9:32 Christophe Raffalli [not found] ` <363F3B0C.C2EACDBC@math.unice.fr> 0 siblings, 1 reply; 2+ messages in thread From: Christophe Raffalli @ 1998-11-02 9:32 UTC (permalink / raw) To: caml-list I have a problem with the following situation: --- class a (x : int) = object val mutable v = x method get = v method set x = v <- x end ;; class b x = object (self) inherit a x as super method getb = self#get method setb = self#set method oldget = super#get end ;; class c x = object (self) inherit a x method getc = self#get method setc = self#set end ;; class d x = object inherit b x inherit c x end ;; (* produces: Warning: the following methods are overriden by the inherited class: get set Warning: this definition of an instance variable v hides a previously defined instance variable of the same name *) let o = new d 2;; o#setb 3;; o#getc;; (* produces 3 *) o#oldget;; (* produces 2 *) --- I understand what is happening, but I would prefer is the default semantic was two merge two values with the same name in an object (giving a type error if their types can not unify). Then one could have a syntactic sugar to change the name of some value: class d x = object inherit b x inherit c x with v -> v' end ;; To ask to rename the value v from b as v'. Together with the possibility to bind a name to acces old method, this will allow a total flexibility (I can always do what I want) which is not the case now. --- Moreover, the follwing would become possible: class d = object inherit b 0 with v -> v0 as b0 inherit b 1 with v -> v1 as b1 inherit b 2 with v -> v2 as b2 end ;; Christophe Raffalli Universite de Savoie PS: do not forget to add "inherit" and "open ... in" for structure in the next release, that will make some program simpler. ^ permalink raw reply [flat|nested] 2+ messages in thread
[parent not found: <363F3B0C.C2EACDBC@math.unice.fr>]
* Re: Sharing inheritance ? [not found] ` <363F3B0C.C2EACDBC@math.unice.fr> @ 1998-11-04 9:43 ` Christophe Raffalli 0 siblings, 0 replies; 2+ messages in thread From: Christophe Raffalli @ 1998-11-04 9:43 UTC (permalink / raw) To: Serge Fantino, caml-list Serge Fantino wrote: > > Christophe Raffalli wrote: > > > I have a problem with the following situation: > > > > ... > > Losque tu définis d, c'est bien ce qui se passe: la variable d'instance v > communeaux classes b & c est "mergée" dans la classe d (d'où le warning > <<this definition of an instance variable v hides a previously defined instance > variable of the same name>>) > La conséquence c'est que getb/getc (resp. set) accèdent à la meme valeur de v, > c'est à dire celle définie pour la classe d; par contre oldget accède à la var. v > définie dans le context de super, qui n'est pas surchargé. Non, il y a deux valeurs de v qui sont stockes (comme le montre le o#oldget). Les methodes sont surchargees de sorte que les methodes de "a" venues du premier "inherit b x" ne sont pas accessibles si on ne les renomme pas a la main (ce que fait "oldget"). Le partage me parait une meilleur solution, avec un renommage explicite sin on ne veut pas partage (je ne vois pas de syntaxe possible pour demander un partage explicite dans la version actuelle). > > Pourquoi alors vouloir que d hérite syntaxiquement de b et c ?Il me semble plus > simple de définir: > class d x = > object > val b = new a x > val c = new a x > method getb=b#get > method setb=b#set > method getc=c#get > method setc=c#set > end les classes b et c peuvent avoir un interet propre et on peut ne pas vouloir reecrire toutes le methodes de d. La situation ou la methode arrive dans mon cas particulier: class a : matrice legere (on peut faire un minimum (produit matrice vecteur)) class b : matrice ligne (on peut iterer une fonction sur les element non nulls de chaque ligne de maniere efficace) class c : matrice colonne (on peut iterer une fonction sur les element non nulls de chaque colonne de maniere efficace) class d : matrice ligne et colonne a la fois. quelques remarque: on a: class transpose_a : a -> a class transpose_b : b -> c class transpose_c : c -> b class transpose_d : d -> d > > --- > > > > Moreover, the follwing would become possible: > > > > class d = > > object > > inherit b 0 with v -> v0 as b0 > > inherit b 1 with v -> v1 as b1 > > inherit b 2 with v -> v2 as b2 > > end > > ;; > > Ici, tu veux que d soit la réunion de 3 instances de b;il n'y a pas de raison que > d hérite 3 fois de b ? > comment accèdes tu ensuite aux valeurs V0,v1,V2 ? > Ici je veux dire on herite 3 fois de b, le v du inherit b 0 s'appelle maintenant v0 et les methodes sont accessibles par b0#xxx. De meme pour les deux autres inherit Christophe Raffalli ^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1998-11-05 7:44 UTC | newest] Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 1998-11-02 9:32 Sharing inheritance ? Christophe Raffalli [not found] ` <363F3B0C.C2EACDBC@math.unice.fr> 1998-11-04 9:43 ` Christophe Raffalli
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox