From: Reed Wilson <cedilla@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] Objects and polymorphic variants
Date: Sat, 2 Feb 2013 15:18:45 -0800 [thread overview]
Message-ID: <CALLFq5SYVOEszYH9jcDTgpA9ZpZb95RXVtsCHfUMZuqqW4ZP2w@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1521 bytes --]
Greetings list,
I've been trying to make object methods that return polymorphic variants,
but it looks a bit stricter than I'm used to.
For example, take the following methods:
method private method_12 = function
| 1 -> `One
| _ -> `Two
method method_123 = function
| 3 -> `Three
| x -> self#method_12 x
method method_124 = function
| 4 -> `Four
| x -> self#method_12 x
This returns the type:
method private method_12 : int -> ([ > `Four | `One | `Three | `Two ] as 'a)
method method_123 : int -> 'a
method method_124 : int -> 'a
and complains that method_123 uses 'a, which is undefined. That's fair
enough, since it's an open type in a non-private method.
However, if I give method_123 or method_124 a closed type signature it will
give the same exact type to the other two methods.
What I really want is a signature like this:
method private method_12 : int -> [ > `One | `Two ]
method method_123 : int -> [ `One | `Two | `Three ]
method method_124 : int -> [ `One | `Two | `Four ]
If I replace method_12 with a function outside the class it works fine, but
for whatever reason method_12 really wants to be the exact same type as
method_123 and method_124.
Is there any way around this typing requirement for methods?
Thanks for any help,
Reed Wilson
PS. I'd rather avoid doing any verbose "post-processing" of the method_12
result, like this:
method_123 = function
| 3 -> `Three
| x -> (match self#method_12 with `One -> `One | `Two -> `Two)
--
ç
[-- Attachment #2: Type: text/html, Size: 2070 bytes --]
next reply other threads:[~2013-02-02 23:18 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-02-02 23:18 Reed Wilson [this message]
2013-02-03 0:13 ` Jeremy Yallop
2013-02-03 1:53 ` Reed Wilson
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=CALLFq5SYVOEszYH9jcDTgpA9ZpZb95RXVtsCHfUMZuqqW4ZP2w@mail.gmail.com \
--to=cedilla@gmail.com \
--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