caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Leonardo Laguna Ruiz <modlfo@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Pattern matching of records
Date: Wed, 16 Sep 2015 14:28:12 +0200	[thread overview]
Message-ID: <55F9605C.9060207@gmail.com> (raw)
In-Reply-To: <55F96004.4010001@gmail.com>

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
>>
>>
>>
>
>


  reply	other threads:[~2015-09-16 12:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-16 12:19 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 [this message]
2015-09-16 19:03 ` Gerd Stolpmann

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=55F9605C.9060207@gmail.com \
    --to=modlfo@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;
as well as URLs for NNTP newsgroup(s).