caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] A question about classes and multiple inheritance
@ 2002-05-23  8:51 Frederic Tronel
  2002-05-23  9:52 ` Jacques Garrigue
  0 siblings, 1 reply; 4+ messages in thread
From: Frederic Tronel @ 2002-05-23  8:51 UTC (permalink / raw)
  To: caml-list

Hello,

I have the following problem.
If I define two classes like this:

class virtual behaviorSpecElement = object (this) 

    /* To be refined by subclasses */
    method virtual localPreBehavior : (string,string) Hashtbl.t  ->
specBehavior
    /* Defined by  specElement */	
    method virtual preBehavior  : (string,string) Hashtbl.t stack ->
specBehavior
end
and
virtual ['a] specElement =

  inherit behaviorSpecElement as super
 
  fun (synchroAccounting : 'a) ->
  object (this)  
	<snip> ....
	method preBehavior bindings =
      		let binding = bindings#top in
      		let myPre = this#localPreBehavior binding in
      		bindings#popSP ;
      		let superPre = super#preBehavior bindings in
      		bindings#pushSP ;
	<snip> ...
  end

The problem is the compiler complains about the fact that 
super has no preBehavior defined. Of course this is the case 
because super is statically resolved and virtual here. 
Instead if I give a dummy definition for method preBehavior in 
behaviorSpecElement like this one:

method localPreBehavior (binding : (string,string) Hashtbl.t) = Null (*
a constructor
of the type specBehavior *)

it seems that super is statically resolved and when I overload
localPreBehavior in subclasses
of specElement, the dummy localPreBehavior is called instead of the
"right" one.

Is there a way to solve this problem ?
"super" must be dynamically resolved at run-time, is it compatible with
multiple inheritance ?


Thanks,

Frederic.
-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] A question about classes and multiple inheritance
  2002-05-23  8:51 [Caml-list] A question about classes and multiple inheritance Frederic Tronel
@ 2002-05-23  9:52 ` Jacques Garrigue
  2002-05-23 11:04   ` Frederic Tronel
  0 siblings, 1 reply; 4+ messages in thread
From: Jacques Garrigue @ 2002-05-23  9:52 UTC (permalink / raw)
  To: Frederic.Tronel; +Cc: caml-list

From: Frederic Tronel <Frederic.Tronel@inrialpes.fr>

> I have the following problem.
> If I define two classes like this:
> 
> class virtual behaviorSpecElement = object (this) 
> 
>     /* To be refined by subclasses */
>     method virtual localPreBehavior : (string,string) Hashtbl.t  ->
> specBehavior
>     /* Defined by  specElement */	
>     method virtual preBehavior  : (string,string) Hashtbl.t stack ->
> specBehavior
> end
> and
> virtual ['a] specElement =
> 
>   inherit behaviorSpecElement as super
>  
>   fun (synchroAccounting : 'a) ->
>   object (this)  
> 	<snip> ....
> 	method preBehavior bindings =
>       		let binding = bindings#top in
>       		let myPre = this#localPreBehavior binding in
>       		bindings#popSP ;
>       		let superPre = super#preBehavior bindings in
>       		bindings#pushSP ;
> 	<snip> ...
>   end

You cannot inherit outside an object, so something is wrong in the
above code. I suppose you just meant inherit inside the object.

> Is there a way to solve this problem ?
> "super" must be dynamically resolved at run-time, is it compatible with
> multiple inheritance ?

By definition super is statically resolved, and self is dynamically
resolved. This is actually the point. 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.

A way to do it is:

class virtual specRecursor =
  object (this)  
        method private virtual localPreBehaviour : ...
        method private virtual superPreBehaviour : ...
	method preBehavior bindings =
      		let binding = bindings#top in
      		let myPre = this#localPreBehavior binding in
      		bindings#popSP ;
      		let superPre = this#superPreBehavior bindings in
      		bindings#pushSP ;
  end

And use it:

class child :
  object
    <declare all methods except localPreBehaviour and superPreBehaviour>
  end =
  object (this)
    inherit parent as super
    method private localPreBehaviour bindings = ...
    method private superPreBehaviour = super#preBehaviour
    inherit specRecursor
  end

The idea is to use localPreBehaviour and superPreBehaviour as
parameters to specElement, making them local to the class, and hide
them from descendants, so that they cannot be modified.

I think that some extension to the current object system might help in
such cases. Like a second abbreviation for self, so that we can bind
statically to its methods, and don't need all this hiding.

Currently it is simpler to redefine explicitly preBehaviour every time.

let preBehavior ~local ~super bindings =
  let binding = bindings#top in
  let myPre = local binding in
  bindings#popSP ;
  let superPre = super bindings in
  bindings#pushSP

class child =
  object (this)
    inherit parent as super
    method private localPreBehaviour bindings = ...
    method preBehavior =
      preBehaviour ~super:super#preBehaviour ~local:this#localPreBehaviour
  end


Jacques Garrigue
-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] A question about classes and multiple inheritance
  2002-05-23  9:52 ` Jacques Garrigue
@ 2002-05-23 11:04   ` Frederic Tronel
  2002-05-24  0:29     ` Jacques Garrigue
  0 siblings, 1 reply; 4+ messages in thread
From: Frederic Tronel @ 2002-05-23 11:04 UTC (permalink / raw)
  To: caml-list

> 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
difficult
to maintain and understand.

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

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

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 ;
     ....
end

For each subclasses of specElement I define:

method superPreBehavior = super#preBehavior

It reduces the code duplication to this single definition. 

Frederic.
-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] A question about classes and multiple inheritance
  2002-05-23 11:04   ` Frederic Tronel
@ 2002-05-24  0:29     ` Jacques Garrigue
  0 siblings, 0 replies; 4+ messages in thread
From: Jacques Garrigue @ 2002-05-24  0:29 UTC (permalink / raw)
  To: Frederic.Tronel; +Cc: caml-list

From: Frederic Tronel <Frederic.Tronel@inrialpes.fr>

> 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.

I think you're confused. "super" is certainly resolved statically in
Java too: try to use super.preBehaviour when your parent class doesn't
define preBehaviour. Except maybe smalltalk, I believe super always
refer to the parent class of the class in which you are defining yur
method.

abstract class A {
    abstract void m();
}

class B extends A {
    void m () {
        super.m();
    }
}

Test.java:7: abstract method m() cannot be accessed directly
        super.m();
             ^
The problem only appears if you are using mixins, but Java has no
mixins, and ocaml multiple inheritance does not really support them
either.

Remark also that in many cases use of higher-order functions and lists
of function can provide a very clean non-oo solution to this kind of
problem.


> 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 ;
>      ....
> end
> 
> For each subclasses of specElement I define:
> 
> method superPreBehavior = super#preBehavior
> 
> It reduces the code duplication to this single definition. 

I don't understand what you are trying to do.
Since superPreBehaviour is not hidden, this will only recurse once:
you might call super#preBehaviour through superPreBehaviour, but in
the superclass you will be calling the same superPreBehaviour, not
super#super#preBehaviour as you seem to be expecting.

  Jacques
-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-05-24  0:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-23  8:51 [Caml-list] A question about classes and multiple inheritance Frederic Tronel
2002-05-23  9:52 ` Jacques Garrigue
2002-05-23 11:04   ` Frederic Tronel
2002-05-24  0:29     ` Jacques Garrigue

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).