Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Martin DeMello <martindemello@gmail.com>
Cc: OCaML List Mailing <caml-list@inria.fr>
Subject: Re: [Caml-list] using a restricted subset of a polymorphic variant
Date: Thu, 10 Sep 2015 18:19:49 +0900	[thread overview]
Message-ID: <D4AA7704-01AB-4163-8A8E-0AB4C9F54D64@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CAFrFfuFJqPfhkWgvdwr+vDsOYbwAi1GhEfOn-Tcttjd1yFiuOA@mail.gmail.com>

On 2015/09/10 18:07, Martin DeMello wrote:
> 
> I'm not sure exactly why this doesn't work:
> 
> type dir = [`North | `South | `East | `West | `Up | `Down]
> 
> let index = function
>   | `North -> 1
>   | `South -> 2
>   | `East -> 3
>   | `West -> 4
>   | `Up -> 5
>   | `Down -> 6
> 
> class foo = object(self)
>   val mutable heading = `Up
> 
>   method flip_heading =
>     heading <- match heading with `Up -> `Down | `Down -> `Up
> 
>   method get_direction (x : dir) = index x
> 
>   method get_heading = self#get_direction heading
> end
> 
> it fails with
> File "test.ml", line 28, characters 42-49:
> Error: This expression has type [ `Down | `Up ]
>        but an expression was expected of type dir
>        The first variant type does not allow tag(s)
>        `East, `North, `South, `West
> 
> but why is that an error? I'd think that any function that accepts type dir should accept type [`Down | `Up] as well. Also, how do I get this to work?


The problem is that OCaml type inference uses unification rather than subtyping.
So [`Down | `Up] and dir are incompatible.

All the answers up to now seem to have assumed that get_direction should only accept [`Up | `Down].
This indeed solves the problem, but may not be what you intended.
If you still want get_direction to acception all directions, then you can either modify get_direction or get_heading
(but no need to change both):

   method get_direction : 'a. ([< dir] as 'a) -> int = index

with this polymorphic method annotation, you can now apply get_direction to heading

  method get_heading = self#get_direction (heading :> dir)

this one uses subtyping to convert heading to dir.

Jacques Garrigue



  parent reply	other threads:[~2015-09-10  9:19 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-10  9:07 Martin DeMello
2015-09-10  9:12 ` Gabriel Scherer
2015-09-10  9:13 ` Edouard Evangelisti
2015-09-10 18:03   ` Martin Jambon
2015-09-10 19:14     ` Martin DeMello
2015-09-10  9:13 ` Christoph Höger
2015-09-10  9:19 ` Jacques Garrigue [this message]
2015-09-10  9:22 ` Romain

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=D4AA7704-01AB-4163-8A8E-0AB4C9F54D64@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=martindemello@gmail.com \
    /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