English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] A question about classes and multiple inheritance
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-05-23 (11:06)
From: Frederic Tronel <Frederic.Tronel@i...>
Subject: Re: [Caml-list] A question about classes and multiple inheritance
> By definition super is statically resolved, and self is dynamically
> resolved. This is actually the point. 

I understood this point by tracking the path of a call to preBehavior.

>In ocaml you don't get two
> independent hierarchies of methods with multiple inheritance: methods
> with same name are merged, taking implementation from last one.
> The only way to get dynamic resolution is to go through self ("this").
> But this seems to be what you are doing anyway, calling
> this#localPreBehaviour. So what's the point in calling
> super#preBehaviour? You certainly don't want to call yourself.
> There's certainly some way to do what you are trying to do, but we
> have to know what you want.
> If what you want is to call a localPreBehaviour for each superclass,
> then it's difficult, because there's no real support for mixins, and
> there's no keyword to say that a method should not appear in
> subclasses.

Each subclass override the definition of localPreBehavior, and I want
preBehavior to call first this#localPreBehavior, and then in turn 
preBehavior method of its upper class (single inheritance),
until it reaches the top level in the hierarchy. 
This is the way it's done in Java for example.
For this to be simple, I need "super" to be resolved dynamically.
But of course, I feel there is a problem in this (contrary to Java
where super is unique) since ocaml supports multiple inheritance and
the binding of superclasses is left to the programmer. 
A default "super" bound to the first inherited class (for example),
dynamically resolved would help building nice applications of objects.
Other scheme are certainly possible, but may lead to code that is
to maintain and understand.

Anyway, for those who are interested, I have found the following way to
solve the

class virtual behaviorSpecElement =
  object (this) 
    method virtual localPreBehavior : (string,string) Hashtbl.t  ->
    method virtual superPreBehavior : (string,string) Hashtbl.t stack ->
    method virtual preBehavior  : (string,string) Hashtbl.t stack ->

and  virtual ['a] specElement =
  fun (synchroAccounting : 'a) ->
  object (this)
  method superPreBehavior bindings = Null (* dummy definition *)
  method preBehavior bindings =
      let binding = bindings#top in
      let myPre = this#localPreBehavior binding in
      bindings#popSP ;
      let superPre = this#superPreBehavior bindings in
      bindings#pushSP ;

For each subclasses of specElement I define:

method superPreBehavior = super#preBehavior

It reduces the code duplication to this single definition. 

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners