caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Suboptimal pattern matching specification
@ 2016-04-07 13:36 Daniel Bünzli
  2016-04-07 16:36 ` Gabriel Scherer
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Bünzli @ 2016-04-07 13:36 UTC (permalink / raw)
  To: OCaml List

Hello,   

Something I run quite often is the following pattern matching  

match v with
| None | Some c when sat c -> expr
| Some …  

which doesn't compile and forces me to write  

match v with  
| None -> expr  
| Some c when sat c -> expr
| Some …  

and leads to code duplication or the introduction of a definition to avoid it.  

Am I missing a syntax bit ?  

Best,

Daniel



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Suboptimal pattern matching specification
  2016-04-07 13:36 [Caml-list] Suboptimal pattern matching specification Daniel Bünzli
@ 2016-04-07 16:36 ` Gabriel Scherer
  0 siblings, 0 replies; 2+ messages in thread
From: Gabriel Scherer @ 2016-04-07 16:36 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: OCaml List

No, indeed you have to use a local definition to avoid code
duplication in this case.

My understanding of the design stance of pattern-matching in OCaml is
as follows: the syntax of patterns is bounded by what can be matched
efficiently. This explains why "when" has a second-class status
(first-class when cannot be matched efficiently); sometimes the user
has to pay for this rigidity. But, on the positive side, it is a
simple and clear stance, and it correlates with the availability of
good tooling, namely exhaustivity and useless-clause warnings.

On Thu, Apr 7, 2016 at 9:36 AM, Daniel Bünzli
<daniel.buenzli@erratique.ch> wrote:
> Hello,
>
> Something I run quite often is the following pattern matching
>
> match v with
> | None | Some c when sat c -> expr
> | Some …
>
> which doesn't compile and forces me to write
>
> match v with
> | None -> expr
> | Some c when sat c -> expr
> | Some …
>
> and leads to code duplication or the introduction of a definition to avoid it.
>
> Am I missing a syntax bit ?
>
> Best,
>
> Daniel
>
>
>
> --
> 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-04-07 16:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-07 13:36 [Caml-list] Suboptimal pattern matching specification Daniel Bünzli
2016-04-07 16:36 ` Gabriel Scherer

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