* [Caml-list] intersections of polymorphic variants
@ 2017-02-09 9:52 Martin DeMello
2017-02-09 9:57 ` Jeremy Yallop
0 siblings, 1 reply; 4+ messages in thread
From: Martin DeMello @ 2017-02-09 9:52 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 608 bytes --]
In the following code:
type color = [`Red | `Blue | `Green | `Black | `White]
let color_index c = match c with
| `Red -> 1
| `Blue -> 2
| `Green -> 3
| `Black -> 4
| `White -> 5
type value = [`Red | `Blue | `Green | `Missing]
type state = Color of int | Missing
let state_of_value v = match v with
| `Red -> Color (color_index `Red)
| `Blue -> Color (color_index `Blue)
| `Green -> Color (color_index `Green)
| `Missing -> Missing
is there any way to write the last function as
let state_of_value v = match v with
| `Red | `Blue | `Green -> ?????
| `Missing -> Missing
martin
[-- Attachment #2: Type: text/html, Size: 2108 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] intersections of polymorphic variants
2017-02-09 9:52 [Caml-list] intersections of polymorphic variants Martin DeMello
@ 2017-02-09 9:57 ` Jeremy Yallop
2017-02-09 10:03 ` Martin DeMello
0 siblings, 1 reply; 4+ messages in thread
From: Jeremy Yallop @ 2017-02-09 9:57 UTC (permalink / raw)
To: Martin DeMello; +Cc: caml-list
On 9 February 2017 at 09:52, Martin DeMello <martindemello@gmail.com> wrote:
> In the following code:
>
> type color = [`Red | `Blue | `Green | `Black | `White]
>
> let color_index c = match c with
> | `Red -> 1
> | `Blue -> 2
> | `Green -> 3
> | `Black -> 4
> | `White -> 5
>
> type value = [`Red | `Blue | `Green | `Missing]
>
> type state = Color of int | Missing
>
> let state_of_value v = match v with
> | `Red -> Color (color_index `Red)
> | `Blue -> Color (color_index `Blue)
> | `Green -> Color (color_index `Green)
> | `Missing -> Missing
>
> is there any way to write the last function as
>
> let state_of_value v = match v with
> | `Red | `Blue | `Green -> ?????
> | `Missing -> Missing
let state_of_value v = match v with
| (`Red | `Blue | `Green) as c -> Color (color_index c)
| `Missing -> Missing
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] intersections of polymorphic variants
2017-02-09 9:57 ` Jeremy Yallop
@ 2017-02-09 10:03 ` Martin DeMello
2017-02-09 15:15 ` Ashish Agarwal
0 siblings, 1 reply; 4+ messages in thread
From: Martin DeMello @ 2017-02-09 10:03 UTC (permalink / raw)
To: Jeremy Yallop; +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 348 bytes --]
On Thu, Feb 9, 2017 at 1:57 AM, Jeremy Yallop <yallop@gmail.com> wrote:
> >
> > let state_of_value v = match v with
> > | `Red | `Blue | `Green -> ?????
> > | `Missing -> Missing
>
> let state_of_value v = match v with
> | (`Red | `Blue | `Green) as c -> Color (color_index c)
> | `Missing -> Missing
>
perfect, thanks!
martin
[-- Attachment #2: Type: text/html, Size: 812 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] intersections of polymorphic variants
2017-02-09 10:03 ` Martin DeMello
@ 2017-02-09 15:15 ` Ashish Agarwal
0 siblings, 0 replies; 4+ messages in thread
From: Ashish Agarwal @ 2017-02-09 15:15 UTC (permalink / raw)
To: Martin DeMello; +Cc: Jeremy Yallop, caml-list
[-- Attachment #1: Type: text/plain, Size: 714 bytes --]
Also you might want abbreviation patterns. If your `value` type included
all `color` values, like this:
type value = [color | `Missing]
then, you could write:
let state_of_value v = match v with
| #color as c -> Color (color_index c)
| `Missing -> Missing
On Thu, Feb 9, 2017 at 5:03 AM, Martin DeMello <martindemello@gmail.com>
wrote:
> On Thu, Feb 9, 2017 at 1:57 AM, Jeremy Yallop <yallop@gmail.com> wrote:
>
>> >
>> > let state_of_value v = match v with
>> > | `Red | `Blue | `Green -> ?????
>> > | `Missing -> Missing
>>
>> let state_of_value v = match v with
>> | (`Red | `Blue | `Green) as c -> Color (color_index c)
>> | `Missing -> Missing
>>
>
> perfect, thanks!
>
> martin
>
[-- Attachment #2: Type: text/html, Size: 1648 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-02-09 15:16 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-09 9:52 [Caml-list] intersections of polymorphic variants Martin DeMello
2017-02-09 9:57 ` Jeremy Yallop
2017-02-09 10:03 ` Martin DeMello
2017-02-09 15:15 ` Ashish Agarwal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox