caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christophe TROESTLER <debian00@tiscali.be>
To: angagon@earthlink.net
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Object typing
Date: Mon, 11 Jul 2005 00:06:42 +0200 (CEST)	[thread overview]
Message-ID: <20050711.000642.115315345.debian00@tiscali.be> (raw)
In-Reply-To: <42D063EC.3040900@earthlink.net>

On Sat, 09 Jul 2005, "Matthew O'Connor" <angagon@earthlink.net> 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.

To understand what does not work, try

class entity  =
object
  method is_a v = (v = `Entity)
end

You get an error message of which the important part is "The method
is_a has type ([> `Entity ] as 'a) -> bool where 'a is unbound".  It
says that the method [is_a] is of type 'a -> bool with a constraint on
'a.  Since you do not want a method of type (forall 'a) 'a -> bool
(written "'a. ([> ] as 'a) -> bool" in your error message), you
parametrize the class by that type 'a (the rest is in interactive
mode):

# class ['a] entity =
object
  method is_a (v:'a) = (v = `Entity)
end;;
  class ['a] entity :
  object constraint 'a = [> `Entity ] method is_a : 'a -> bool end

# class ['a] predator =
object
  inherit ['a] entity as super
  method is_a (v:'a) = (v = `Predator) || super#is_a v
end;;
  class ['a] predator :
  object
    constraint 'a = [> `Entity | `Predator ]
    method is_a : 'a -> bool
  end

# let ent = new predator;;
val ent : _[> `Entity | `Predator ] predator
# ent#is_a `Predator;;
- : bool = true
# ent#is_a `Entity;;
- : bool = true
# ent#is_a `Chicken;;
- : bool = false


P.S.  Notice the "_" in "_[> `Entity | `Predator ]".  It means that
the full type is yet to be determined.  Close it before the end of
your program.


  parent reply	other threads:[~2005-07-10 22:10 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-09 23:55 Matthew O'Connor
2005-07-10  0:20 ` [Caml-list] " Pietro Abate
2005-07-10  6:19   ` Matthew O'Connor
2005-07-10 18:25 ` Stephane Glondu
2005-07-10 22:06 ` Christophe TROESTLER [this message]
2005-07-11  6:23   ` Matthew O'Connor
2005-07-11  7:18   ` Remi Vanicat
2005-07-11  8:22     ` Jacques Garrigue
2005-07-11  9:16       ` Marcin 'Qrczak' Kowalczyk

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=20050711.000642.115315345.debian00@tiscali.be \
    --to=debian00@tiscali.be \
    --cc=angagon@earthlink.net \
    --cc=caml-list@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;
as well as URLs for NNTP newsgroup(s).