From: Martin DeMello <martindemello@gmail.com>
To: Martin Jambon <martin.jambon@ens-lyon.org>
Cc: OCaml Mailing List <caml-list@inria.fr>,
Edouard Evangelisti <edouardevangelisti@gmail.com>
Subject: Re: [Caml-list] using a restricted subset of a polymorphic variant
Date: Thu, 10 Sep 2015 12:14:21 -0700 [thread overview]
Message-ID: <CAFrFfuEnqpXHGuc8ceBAPjGZpLEoSN+sD5mx3_6Ydk1yKr52EQ@mail.gmail.com> (raw)
In-Reply-To: <55F1C5EE.7010205@ens-lyon.org>
[-- Attachment #1: Type: text/plain, Size: 2605 bytes --]
Thanks all! I was trying to express the fact that heading could only be `Up
or `Down but should act as a direction otherwise; Romain is right that I
was expecting the coercion to happen automatically (too much time in other
languages :)). Annotating
val mutable heading : [`Up | `Down] = `Up
and then coercing it when I pass it as a direction worked nicely.
martin
On Thu, Sep 10, 2015 at 11:03 AM, Martin Jambon <martin.jambon@ens-lyon.org>
wrote:
> I'd like to add that this a good systematic way to avoid confusing error
> messages when dealing with polymorphic variants (or objects). Just add type
> annotations on variables and function parameters which are polymorphic
> variants (or objects).
>
> Here only one extra annotation would be needed, on the heading value:
>
> val mutable heading : dir = `Up
> method flip_heading = ...
> method get_direction (x : dir) = ...
> method get_heading = ...
>
> The error message becomes closer to the actual error:
>
> File "toto.ml", line 15, characters 15-61:
> Warning 8: this pattern-matching is not exhaustive.
> Here is an example of a value that is not matched:
> (`West|`South|`North|`East)
>
> Martin
>
> On 09/10/2015 02:13 AM, Edouard Evangelisti wrote:
>
>> Dear Martin,
>>
>> In your example, heading is not of type dir.
>> However, this would work :
>>
>> val mutable heading : dir = `Up
>>
>> Regards,
>> Edouard
>>
>>
>> 2015-09-10 10:07 GMT+01:00 Martin DeMello <martindemello@gmail.com
>> <mailto:martindemello@gmail.com>>:
>>
>> 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 <http://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?
>>
>> martin
>>
>>
>>
[-- Attachment #2: Type: text/html, Size: 3864 bytes --]
next prev parent reply other threads:[~2015-09-10 19:14 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 [this message]
2015-09-10 9:13 ` Christoph Höger
2015-09-10 9:19 ` Jacques Garrigue
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=CAFrFfuEnqpXHGuc8ceBAPjGZpLEoSN+sD5mx3_6Ydk1yKr52EQ@mail.gmail.com \
--to=martindemello@gmail.com \
--cc=caml-list@inria.fr \
--cc=edouardevangelisti@gmail.com \
--cc=martin.jambon@ens-lyon.org \
/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