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] examples of heterogenous collections (containers ?)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-03-31 (06:45)
From: briand@a...
Subject: Re: [Caml-list] examples of heterogenous collections (containers ?)
>>>>> "Matt" == Matt Gushee <mgushee@havenrock.com> writes:

  Matt> This will work if all the classes have identical signatures. Of course,
  Matt> that's not the case for most non-trivial examples.

The classes have identical signatures, i.e. all have exactly the same
methods and variables ??

  Matt> Probably not. First of all, you will need to cast the objects at the
  Matt> point where you put them into a list together ... thus, at the point
  Matt> where you do the method call, the cast has already been done. Second,
  Matt> you don't necessarily want to cast them to the base class.

Well that makes sense, if I can figure out the part about all the
classes havin g the same signature.

  Matt> As some people never tire of pointing out ;-), inheritance is
  Matt> not subtyping. Actually, I haven't myself figured out all the
  Matt> implications of that statement, nor learned the theoretical

Yes, I've been seeing that, and amazingly it's actually starting to
sink in.  There are some notes from the ocaml page which discuss this
relatively well : Didier Remy's APPSEM course notes on Objective Caml.

  Matt> justification for it--but at any rate, in OCaml class types
  Matt> are completely independent of inheritance
  Matt> relationships. That's a good thing because it means you can
  Matt> define a class type and arbitrarily apply it to any class that
  Matt> provides *at least* the methods specified in the type
  Matt> signature, regardless of inheritance. E.g.:

  Matt>   class type x =
  Matt>     object
  Matt>       method a : unit
  Matt>       method b : int -> bool
  Matt>     end

  Matt> ... and so on.

??? So it's example time :

class virtual base =

class A1 = object(self)
inherit base
method x1 = ...
method x2 = ...

class A2 = object(self)
inherit base
method x1 = ...
method x2 = ...
method x3 = ...

So I should be able to do :

let a = new A1 in
let b = new A2 in
  (a :> base) :: (b :> base) :: []

right ?  now my list has type "base list".

So I immediately see the problem here.

My iter code could try to invoke method x3 on the A1 object in the
list as I'm iterating through. oops.

So it seems to me that I need a "stronger" method to enforce safety,
perhaps implementing x1 as a virtual method in base ?


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