Skip to content

Instantly share code, notes, and snippets.

@dtchepak
Last active August 29, 2015 14:00
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 dtchepak/11080115 to your computer and use it in GitHub Desktop.
Save dtchepak/11080115 to your computer and use it in GitHub Desktop.
//Experiment, based on http://withouttheloop.com/articles/2014-04-18-fsharp-csharp-statemachines/
open System
type Particulars = { buyer: string; seller: string }
type Draft = private Draft of Particulars
type Approved = private Approved of (Particulars * DateTime)
type Historical = private Historical of (Particulars * DateTime)
let createDraft = Draft
let approve timestamp (Draft p) = Approved (p, timestamp)
let retire timestamp (Approved (p,_)) = Historical (p, timestamp)
// usage
let contract = createDraft { buyer = "Acme"; seller = "Initech" }
let approved = approve DateTime.Now contract
let retired = retire DateTime.Now approved
retired |> printfn "%+A"
type Contract = Choice<Draft, Approved, Historical>
//let invalid = approve DateTime.Now approved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment