Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6199899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:16:19+00:00 2026-05-24T04:16:19+00:00

Have a look at this F#/OCaml code: type AllPossible = | A of int

  • 0

Have a look at this F#/OCaml code:

type AllPossible =
    | A of int
    | B of int*int
    | ...
    | Z of ...

let foo x =
    ....
    match x with
    | A(value) | B(value,_) ->                   (* LINE 1 *)
        (* do something with the first (or only, in the case of A) value *)
        ...
        (* now do something that is different in the case of B *)
        let possibleData = 
            match x with
            | A(a) -> bar1(a)
            | B(a,b) -> bar2(a+b)
            | _ -> raise Exception    (* the problem - read below *)
        (* work with possibleData *)
    ...
    | Z -> ...

So what is the problem?
In function foo, we pattern match against a big list of types.
Some of the types share functionality – e.g. they have common
work to do, so we use “|A | B ->” in LINE 1, above.
We read the only integer (in the case of A), or the first integer
(in the case of B) and do something with it.

Next, we want to do something that is completely different, depending
on whether we work on A or B (i.e. call bar1 or bar2).
We now have to pattern match again, and here’s the problem: In this
nested pattern match, unless we add a ‘catchAll’ rule (i.e. ‘_’),
the compiler complains that we are missing cases – i.e. it doesn’t
take into account that only A and B can happen here.

But if we add the catchAll rule, then we have a far worse problem:
if at some point we add more types in the list of LINE1
(i.e. in the line ‘|A | B ->’ … then the compiler will NOT help
us in the nested match – the ‘_’ will catch them, and a bug will
be detected at RUNTIME. One of the most important powers of
pattern matching – i.e. detecting such errors at compile-time – is lost.

Is there a better way to write this kind of code, without having
to repeat whatever work is shared amongst A and B in two separate
rules for A and B? (or putting the A-and-B common work in a function
solely created for the purpose of “local code sharing” between A and B?)

EDIT: Note that one could argue that the F# compiler’s behaviour is buggy in this case –
it should be able to detect that there’s no need for matching beyond A and B
in the nested match.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:16:20+00:00Added an answer on May 24, 2026 at 4:16 am

    If the datatype is set in stone – I would also prefer local function.

    Otherwise, in OCaml you could also enjoy open (aka polymorphic) variants :

    type t = [`A | `B | `C]
    let f = function
    | (`A | `B as x) ->
      let s = match x with `A -> "a" | `B -> "b" in
      print_endline s
    | `C -> print_endline "ugh"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, have a look at this code: Dictionary<int,int> dict = Dictionary<int,int>(); dict[3] = 1;
Please have a look on this code: #include <unistd.h> #include <stdlib.h> #include <stdio.h> int
Have a look at this code snippet struct S{ int i; int j;}; int
Have a look at this code: def closure(): value = False def method_1(): value
Have a look at this code: #include <iostream> using namespace std; int main() {
Please have a look at this code: Card *first = [[Card alloc] initWithFace:@Ace andSuit:@Hearts];
Could you have a look through this code and let me know whats wrong?
first, have to look at this dragndrop ( http://code.google.com/p/jquery-drag-and-drop/ ) what i want to
have a look at this code: #include<stdio.h> #include <unistd.h> int main() { int pipefd[2],n;
Have a look at this code snippet: int i = 10; int *pi =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.