Skip to content

Instantly share code, notes, and snippets.

@davidglassborow
Created June 1, 2022 14:54
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 davidglassborow/7da045c580a6917863c632be550b968c to your computer and use it in GitHub Desktop.
Save davidglassborow/7da045c580a6917863c632be550b968c to your computer and use it in GitHub Desktop.
Alternate version of Private DU constructor
// http://www.fssnip.net/ma/title/Alternate-version-of-Private-DU-constructor
module Email =
type EmailAddress =
private
| ValidEmail of string
| InvalidEmail of string
let ofString = function
| "validEmail" -> ValidEmail "validEmail"
| invalid -> InvalidEmail invalid
let (|ValidEmail|InvalidEmail|) = function
| ValidEmail email -> ValidEmail email
| InvalidEmail email -> InvalidEmail email
open Email
let invalid = Email.ofString "invalid"
let valid = Email.ofString "validEmail"
match invalid with
| InvalidEmail invalid -> printfn "invalid was InvalidEmail %s" invalid
| ValidEmail valid -> printfn "invalid was ValidEmail %s" valid
match valid with
| InvalidEmail invalid -> printfn "valid was InvalidEmail %s" invalid
| ValidEmail valid -> printfn "valid was ValidEmail %s" valid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment