caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: william.newman@airmail.net
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] difficulties narrowing OO types
Date: Thu, 15 Nov 2001 15:43:35 +0900	[thread overview]
Message-ID: <20011115154335H.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20011115100557Y.garrigue@kurims.kyoto-u.ac.jp>

From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>

> Since the problem is with inheritance, and not with subtyping, there
> are workarounds to define such classes. Basically the idea is to keep
> the type of self as a parameter to the class.

Actually, I realized after posting my previous message that it was a
bit far-fetched for simple cases. If you just want to be able to list
objects which have the type of the current class, then recursion on
the type of self is enough and simpler.

Here is the same example as before:

class base ~pos ~friends =
  object (_ : 'a)
    method pos : int = pos
    method friends : 'a list = friends
  end

class extended ~pos ~friends ~name =
  object
    inherit base ~pos ~friends
    method name : string = name
  end

Which give types:
class base :
  pos:int ->
  friends:('a list as 'b) ->
  object ('a) method friends : 'b method pos : int end
class extended :
  pos:int ->
  friends:('a list as 'b) ->
  name:string ->
  object ('a) method friends : 'b method name : string method pos : int end

And you can even coerce with semi-explicit coercions:
# let e = new extended ~pos:1 ~friends:[] ~name:"e";;
val e : extended = <obj>
# let b = new base ~pos:2 ~friends:[(e :> base)];;
val b : base = <obj>

Hope this helps.

Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


      reply	other threads:[~2001-11-15  6:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-14 17:17 William Harold Newman
2001-11-15  1:05 ` Jacques Garrigue
2001-11-15  6:43   ` Jacques Garrigue [this message]

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=20011115154335H.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=william.newman@airmail.net \
    /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).