Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Created January 19, 2021 14:29
Show Gist options
  • Select an option

  • Save isaacabraham/bc945ade32911f27d80b237657b58d8c to your computer and use it in GitHub Desktop.

Select an option

Save isaacabraham/bc945ade32911f27d80b237657b58d8c to your computer and use it in GitHub Desktop.
/// An example union.
type Team = Tottenham | Spurs | THFC
module Lists =
open FSharp.Reflection
/// Creates an array of all cases of a given union 'T.
let makeAll<'T> =
FSharpType.GetUnionCases typeof<'T>
|> Array.map(fun info -> FSharpValue.MakeUnion(info, Array.empty) :?> 'T)
/// All Team cases - calculated once
let Teams = makeAll<Team>
type Team with
/// For added ease of use, you could add this static extension member onto Team
static member All = Lists.Teams
let allTeams = Lists.Teams
let allTeamsAgain = Team.All
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment