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
Object typing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-07-10 (18:26)
From: Stephane Glondu <Stephane.Glondu@c...>
Subject: Re: [Caml-list] Object typing
On Saturday 09 July 2005 16:55, Matthew O'Connor wrote:
> Here is a version of what I would like to be able to do, but can't or
> don't know how.  I would like to use polymorphic variants to enable
> easier extensibility of the hierarchy.
> [...]
> I'm at a loss as how to proceed.  Any other suggestions or ideas or
> better solutions would be greatly appreciated.  Thank you.

How about:

(* test.ml *)
class ['a] entity =
  method is_a (v : 'a) =
    if v = `Entity then true
    else false

class ['a] predator =
  inherit ['a] entity as super
  method is_a v =
    if v = `Predator then true
    else super#is_a v

let ent = new predator;;
ent#is_a `Predator;; (* returns true *)
ent#is_a `Entity;; (* returns true *)
ent#is_a `Chicken;; (* returns false *)

Stephane Glondu.