* [Caml-list] Pattern matching of records
@ 2015-09-16 12:19 Leonardo Laguna Ruiz
2015-09-16 12:26 ` Nicolas Ojeda Bar
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Leonardo Laguna Ruiz @ 2015-09-16 12:19 UTC (permalink / raw)
To: caml-list
I have the following type
(* File: S.ml *)
module S = struct
type s =
{
a : int;
b : int;
}
end
I I have found that for creating records I can do as follows:
(* File: main.ml *)
open S
let s0 = S.{ a = 0; b = 0}
However this does not work for pattern matching:
(* this does not work *)
match s0 with
| S.{ a = 0 ; b = 0 } -> true
| _ -> false
These two alternatives work, but one with a warning and the other
(depending on the type) is too verbose:
(* this produces a warning *)
match s0 with
| { a = 0 ; b = 0 } -> true
| _ -> false
(* this works *)
match s0 with
| { S.a = 0 ; S.b = 0 } -> true
| _ -> false
Is there any other way of writing pattern matches as compact as S.{ a =
0 ; b = 0 } ? (which is consistent to the construction of the same value)
I know that it's possible to do:
let open S in
match s0 with
| { a = 0 ; b = 0 } -> true
| _ -> false
but the main reason I don't do it is because I have more records like:
match s0,k0 with
| { S.a = 0 }, {K.a = 0 } -> true
Best regards,
Leonardo
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Pattern matching of records
2015-09-16 12:19 [Caml-list] Pattern matching of records Leonardo Laguna Ruiz
@ 2015-09-16 12:26 ` Nicolas Ojeda Bar
2015-09-16 12:26 ` Thibault Suzanne
2015-09-16 19:03 ` Gerd Stolpmann
2 siblings, 0 replies; 5+ messages in thread
From: Nicolas Ojeda Bar @ 2015-09-16 12:26 UTC (permalink / raw)
To: Leonardo Laguna Ruiz; +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 1671 bytes --]
Hi Leonardo,
You only need to fully qualify _one_ label in order to disambiguate the
record type, for example:
match s0 with
| {S.a = 0; b = 0} -> true
| _ -> false
Cheers,
Nicolas
On Wed, Sep 16, 2015 at 2:19 PM, Leonardo Laguna Ruiz <modlfo@gmail.com>
wrote:
>
> I have the following type
>
> (* File: S.ml *)
> module S = struct
> type s =
> {
> a : int;
> b : int;
> }
> end
>
>
> I I have found that for creating records I can do as follows:
>
> (* File: main.ml *)
> open S
> let s0 = S.{ a = 0; b = 0}
>
>
> However this does not work for pattern matching:
>
> (* this does not work *)
> match s0 with
> | S.{ a = 0 ; b = 0 } -> true
> | _ -> false
>
>
> These two alternatives work, but one with a warning and the other
> (depending on the type) is too verbose:
>
> (* this produces a warning *)
> match s0 with
> | { a = 0 ; b = 0 } -> true
> | _ -> false
>
> (* this works *)
> match s0 with
> | { S.a = 0 ; S.b = 0 } -> true
> | _ -> false
>
>
> Is there any other way of writing pattern matches as compact as S.{ a = 0
> ; b = 0 } ? (which is consistent to the construction of the same value)
>
> I know that it's possible to do:
>
> let open S in
> match s0 with
> | { a = 0 ; b = 0 } -> true
> | _ -> false
>
> but the main reason I don't do it is because I have more records like:
>
> match s0,k0 with
> | { S.a = 0 }, {K.a = 0 } -> true
>
>
>
> Best regards,
>
> Leonardo
>
>
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
[-- Attachment #2: Type: text/html, Size: 2753 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Pattern matching of records
2015-09-16 12:19 [Caml-list] Pattern matching of records Leonardo Laguna Ruiz
2015-09-16 12:26 ` Nicolas Ojeda Bar
@ 2015-09-16 12:26 ` Thibault Suzanne
2015-09-16 12:28 ` Leonardo Laguna Ruiz
2015-09-16 19:03 ` Gerd Stolpmann
2 siblings, 1 reply; 5+ messages in thread
From: Thibault Suzanne @ 2015-09-16 12:26 UTC (permalink / raw)
To: caml-list
There is no need to specify the origin module in front of each field :
match s0 with
| { S.a = 0; b = 0 } -> true
| _ -> false
Le 16/09/2015 14:19, Leonardo Laguna Ruiz a écrit :
>
> I have the following type
>
> (* File: S.ml *)
> module S = struct
> type s =
> {
> a : int;
> b : int;
> }
> end
>
>
> I I have found that for creating records I can do as follows:
>
> (* File: main.ml *)
> open S
> let s0 = S.{ a = 0; b = 0}
>
>
> However this does not work for pattern matching:
>
> (* this does not work *)
> match s0 with
> | S.{ a = 0 ; b = 0 } -> true
> | _ -> false
>
>
> These two alternatives work, but one with a warning and the other
> (depending on the type) is too verbose:
>
> (* this produces a warning *)
> match s0 with
> | { a = 0 ; b = 0 } -> true
> | _ -> false
>
> (* this works *)
> match s0 with
> | { S.a = 0 ; S.b = 0 } -> true
> | _ -> false
>
>
> Is there any other way of writing pattern matches as compact as S.{ a
> = 0 ; b = 0 } ? (which is consistent to the construction of the same
> value)
>
> I know that it's possible to do:
>
> let open S in
> match s0 with
> | { a = 0 ; b = 0 } -> true
> | _ -> false
>
> but the main reason I don't do it is because I have more records like:
>
> match s0,k0 with
> | { S.a = 0 }, {K.a = 0 } -> true
>
>
>
> Best regards,
>
> Leonardo
>
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Pattern matching of records
2015-09-16 12:26 ` Thibault Suzanne
@ 2015-09-16 12:28 ` Leonardo Laguna Ruiz
0 siblings, 0 replies; 5+ messages in thread
From: Leonardo Laguna Ruiz @ 2015-09-16 12:28 UTC (permalink / raw)
To: caml-list
Great! thank you very much guys!
Leonardo
On 2015-09-16 14:26, Thibault Suzanne wrote:
> There is no need to specify the origin module in front of each field :
>
> match s0 with
> | { S.a = 0; b = 0 } -> true
> | _ -> false
>
> Le 16/09/2015 14:19, Leonardo Laguna Ruiz a écrit :
>>
>> I have the following type
>>
>> (* File: S.ml *)
>> module S = struct
>> type s =
>> {
>> a : int;
>> b : int;
>> }
>> end
>>
>>
>> I I have found that for creating records I can do as follows:
>>
>> (* File: main.ml *)
>> open S
>> let s0 = S.{ a = 0; b = 0}
>>
>>
>> However this does not work for pattern matching:
>>
>> (* this does not work *)
>> match s0 with
>> | S.{ a = 0 ; b = 0 } -> true
>> | _ -> false
>>
>>
>> These two alternatives work, but one with a warning and the other
>> (depending on the type) is too verbose:
>>
>> (* this produces a warning *)
>> match s0 with
>> | { a = 0 ; b = 0 } -> true
>> | _ -> false
>>
>> (* this works *)
>> match s0 with
>> | { S.a = 0 ; S.b = 0 } -> true
>> | _ -> false
>>
>>
>> Is there any other way of writing pattern matches as compact as S.{ a
>> = 0 ; b = 0 } ? (which is consistent to the construction of the same
>> value)
>>
>> I know that it's possible to do:
>>
>> let open S in
>> match s0 with
>> | { a = 0 ; b = 0 } -> true
>> | _ -> false
>>
>> but the main reason I don't do it is because I have more records like:
>>
>> match s0,k0 with
>> | { S.a = 0 }, {K.a = 0 } -> true
>>
>>
>>
>> Best regards,
>>
>> Leonardo
>>
>>
>>
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Pattern matching of records
2015-09-16 12:19 [Caml-list] Pattern matching of records Leonardo Laguna Ruiz
2015-09-16 12:26 ` Nicolas Ojeda Bar
2015-09-16 12:26 ` Thibault Suzanne
@ 2015-09-16 19:03 ` Gerd Stolpmann
2 siblings, 0 replies; 5+ messages in thread
From: Gerd Stolpmann @ 2015-09-16 19:03 UTC (permalink / raw)
To: Leonardo Laguna Ruiz; +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 1823 bytes --]
Am Mittwoch, den 16.09.2015, 14:19 +0200 schrieb Leonardo Laguna Ruiz:
> I have the following type
>
> (* File: S.ml *)
> module S = struct
> type s =
> {
> a : int;
> b : int;
> }
> end
>
>
> I I have found that for creating records I can do as follows:
>
> (* File: main.ml *)
> open S
> let s0 = S.{ a = 0; b = 0}
>
>
> However this does not work for pattern matching:
>
> (* this does not work *)
> match s0 with
> | S.{ a = 0 ; b = 0 } -> true
> | _ -> false
You are thinking the right way, but you run into a well-known
shortcoming: http://caml.inria.fr/mantis/view.php?id=6656
Gerd
>
>
> These two alternatives work, but one with a warning and the other
> (depending on the type) is too verbose:
>
> (* this produces a warning *)
> match s0 with
> | { a = 0 ; b = 0 } -> true
> | _ -> false
>
> (* this works *)
> match s0 with
> | { S.a = 0 ; S.b = 0 } -> true
> | _ -> false
>
>
> Is there any other way of writing pattern matches as compact as S.{ a =
> 0 ; b = 0 } ? (which is consistent to the construction of the same value)
>
> I know that it's possible to do:
>
> let open S in
> match s0 with
> | { a = 0 ; b = 0 } -> true
> | _ -> false
>
> but the main reason I don't do it is because I have more records like:
>
> match s0,k0 with
> | { S.a = 0 }, {K.a = 0 } -> true
>
>
>
> Best regards,
>
> Leonardo
>
>
>
--
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de
My OCaml site: http://www.camlcity.org
Contact details: http://www.camlcity.org/contact.html
Company homepage: http://www.gerd-stolpmann.de
------------------------------------------------------------
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 473 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-09-16 19:03 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-16 12:19 [Caml-list] Pattern matching of records Leonardo Laguna Ruiz
2015-09-16 12:26 ` Nicolas Ojeda Bar
2015-09-16 12:26 ` Thibault Suzanne
2015-09-16 12:28 ` Leonardo Laguna Ruiz
2015-09-16 19:03 ` Gerd Stolpmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox