Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Last active October 14, 2019 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/775f812732bf8058e2a2cc9f579e0d5f to your computer and use it in GitHub Desktop.
Save isaacabraham/775f812732bf8058e2a2cc9f579e0d5f to your computer and use it in GitHub Desktop.
Some examples of pattern matching with explicit options
type SurfaceOrientation = Rotation270 = 0 | Rotation180 = 1 | Rotation90 = 2 | Rotation0 = 3
type DisplayOrientation = Rotation270 | Rotation180 | Rotation90 | Rotation0 | Unknown
type Display = { Rotation : SurfaceOrientation }
let getDefaultDisplay() : Display option = failwith ""
let display = getDefaultDisplay()
// 1. Does not compile! Can't access Rotation directly as the display value is optional.
match display.Rotation with
| SurfaceOrientation.Rotation270 -> DisplayOrientation.Rotation270
| SurfaceOrientation.Rotation180 -> DisplayOrientation.Rotation180
| SurfaceOrientation.Rotation90 -> DisplayOrientation.Rotation90
| SurfaceOrientation.Rotation0 -> DisplayOrientation.Rotation0
| _ -> Unknown
// 2. Two-step match - first on optionality, then on comparison
match display with
| Some display ->
match display.Rotation with
| SurfaceOrientation.Rotation270 -> DisplayOrientation.Rotation270
| SurfaceOrientation.Rotation180 -> DisplayOrientation.Rotation180
| SurfaceOrientation.Rotation90 -> DisplayOrientation.Rotation90
| SurfaceOrientation.Rotation0 -> DisplayOrientation.Rotation0
| _ -> DisplayOrientation.Unknown
| None ->
DisplayOrientation.Unknown
// 3. One-step match, but optionality still explicit - this is closest equivalent to original C#?
match display with
| Some { Rotation = SurfaceOrientation.Rotation270 } -> DisplayOrientation.Rotation270
| Some { Rotation = SurfaceOrientation.Rotation180 } -> DisplayOrientation.Rotation180
| Some { Rotation = SurfaceOrientation.Rotation90 } -> DisplayOrientation.Rotation90
| Some { Rotation = SurfaceOrientation.Rotation0 } -> DisplayOrientation.Rotation0
| Some { Rotation = _ } -> DisplayOrientation.Unknown
| None -> DisplayOrientation.Unknown
// 4. Removal of "Unknown" case, using None instead
match display with
| Some { Rotation = SurfaceOrientation.Rotation270 } -> Some DisplayOrientation.Rotation270
| Some { Rotation = SurfaceOrientation.Rotation180 } -> Some DisplayOrientation.Rotation180
| Some { Rotation = SurfaceOrientation.Rotation90 } -> Some DisplayOrientation.Rotation90
| Some { Rotation = SurfaceOrientation.Rotation0 } -> Some DisplayOrientation.Rotation0
| Some { Rotation = _ } -> None
| None -> None
// 5. Using option map / bind
display
|> Option.bind(function
| { Rotation = SurfaceOrientation.Rotation270 } -> Some DisplayOrientation.Rotation270
| { Rotation = SurfaceOrientation.Rotation180 } -> Some DisplayOrientation.Rotation180
| { Rotation = SurfaceOrientation.Rotation90 } -> Some DisplayOrientation.Rotation90
| { Rotation = SurfaceOrientation.Rotation0 } -> Some DisplayOrientation.Rotation0
| { Rotation = _ } -> None)
// 6. This is what I *thought* the original C# sample evaluated to.
match display with
| Some { Rotation = SurfaceOrientation.Rotation270 } -> Some DisplayOrientation.Rotation270
| Some { Rotation = SurfaceOrientation.Rotation180 } -> Some DisplayOrientation.Rotation180
| Some { Rotation = SurfaceOrientation.Rotation90 } -> Some DisplayOrientation.Rotation90
| Some { Rotation = SurfaceOrientation.Rotation0 } -> Some DisplayOrientation.Rotation0
| Some { Rotation = _ } -> Some DisplayOrientation.Unknown
| None -> None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment