Skip to content

Instantly share code, notes, and snippets.

@jhewlett
Last active January 3, 2019 14:30
Show Gist options
  • Save jhewlett/e1775dffb6552aff28883936536e42c1 to your computer and use it in GitHub Desktop.
Save jhewlett/e1775dffb6552aff28883936536e42c1 to your computer and use it in GitHub Desktop.
Nested match
let findThing id =
match findThingA id with
| None -> None
| Some thingA ->
match findThingB id with
| None -> None
| Some thingB ->
combine thingA thingB
@Thorium
Copy link

Thorium commented Jan 3, 2019

If you don't like this and you prefer the C#/Java-style early returns, you can write:

let findThing id =
   if not hasA then None
   else

   if not hasB then None
   else

   if not hasC then None
   else

   // logic continues without nesting...
   combine A B C

This is not a problem as the compiler will yell what to fix if you change the return type of your function in one place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment