Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Jørgen Hermanrud Fjeld" <jhf@hex.no>
To: caml-list@yquem.inria.fr
Subject: Possibility of Nested Classes and Nested Inheritance?
Date: Thu, 16 Dec 2004 15:59:07 +0100	[thread overview]
Message-ID: <20041216145907.GB5599@hex.no> (raw)

[-- Attachment #1: Type: text/plain, Size: 3773 bytes --]

Hi, 
I just read about the work by Nystrom, Chong and Myers on nested
inheritance, specifically the article "Scalable Extensibility via Nested
Inheritance".

The article does demonstrate fascinating, to me, use of inheritance, and
I wonder if it is possible to do something similar and object-oriented in OCaml.

To do something similar would, according to my understanding, require
both inner classes and super-class polymorphism.
In understand inner classes as implicitly polymorphic with respect to the enclosing class,
and polymorphism on the super class as the practical ability to extend
the type hierarchy upwards.

Do you know of any work that relate nested inheritance to OCaml, or that
address the similar issuesof inner classes and super-class polymorphism?

I have tried to search the mailing list history, but I have not
found relevant threads for these issues.

It seems to me that inner classes can always be written as parametric
classes, which means that OCaml could easily support inner classes. 
Is this correct? Are there other intrinsic reasons why OCaml does not
have inner classes, except of course that it would take an effort to
implement, which I understand.

Super-class polymorphism seems like a different beast, and I would very
much appreciate any ideas, especially theoretical ideas, on how that 
would interact with the OCaml type system.

I imagine the following OCaml'ish example:
class a =
    class b = object ... end
    class c = object inherit b ... end
    object
    ...
end

class d =
    class b' = object inherit b ... end
    (* The following is implicit
    class c' = object inherit b inherit b'  inherit c ... end *)
    object
      inherit a
      ...
end

The inner classes are parametrised by the outer class, thus for the
class a this could be written instead:

class a =
    object
    ...
end
class ['a] b = object ... end
class ['a] c = object inherit ['a] b ... end

Here i use 'a because the examples is from a nominal type system, 
as the name 'a suggests, although this is just coincidental for O'Caml.

The class d is not such a clear case to write out, here is a try:

class d =
    object
      inherit a
      ...
end

class ['d] b' = object inherit ['d] b ... end
(* The following is implicit
class ['d] c' = 
    object 
	inherit ['d] b' 
	inherit ['d] c 
	... 
    end 
*)

It is this implicit part that I suspect could use super-class
polymorphism, because then the class c could be rewritten as:

class ['a,'b] c = 
    object 
	inherit ['a] 'b 
	... 
    end

and then the class c' would be the same as c, except that c' 
is parametrised by [d,b'], while c is parametrised by [a,b].

With name shadowing of classes and explicit polymorphism 
code could be written as:

class a =
    class ['a] b = object ... end
    class ['a,'b] c = object inherit 'b ... end
    object
    ...
end

class d =
    class ['a] b = object inherit b ... end
    (* The following is implicit because b is connected to 'b by magic ;-)
    class ['a,'b] = object inherit b' ... end 
    *)
    object
      inherit a
      ...
end

I hope this demonstrates the idea.

The following code is from the paper by Nystrom, Chong and Myers, 
to give a sample of their intuition:
class A {
  class B { int x; }
  class C extends B {...}
  int m(B b) { return b.x }
  C n() { return new C(); }
  }

class A2 extends A {
  class B {int y; } 
  int m(B b) {return b.x + b.y }
  }



-- 
Sincerely | Homepage:
Jørgen    | http://www.hex.no/jhf
          | Public GPG key:
          | http://www.hex.no/jhf/key.txt

Mystics always hope that science will some day overtake them.
		-- Booth Tarkington


[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

             reply	other threads:[~2004-12-16 15:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-16 14:59 Jørgen Hermanrud Fjeld [this message]
2004-12-16 21:50 ` [Caml-list] " John Prevost
2004-12-17  1:31 ` Jacques Garrigue

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20041216145907.GB5599@hex.no \
    --to=jhf@hex.no \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox