Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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 =
object
  method is_a (v : 'a) =
    if v = `Entity then true
    else false
end

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

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


Stephane Glondu.