4 people like it.

Alternate version of Private DU constructor

A different way of expressing private DU constructors.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
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
type EmailAddress =
  private | ValidEmail of string
          | InvalidEmail of string

Full name: Script.Email.EmailAddress
union case EmailAddress.ValidEmail: string -> EmailAddress
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
union case EmailAddress.InvalidEmail: string -> EmailAddress
val ofString : _arg1:string -> EmailAddress

Full name: Script.Email.ofString
val invalid : string
val email : string
module Email

from Script
val invalid : EmailAddress

Full name: Script.invalid
val valid : EmailAddress

Full name: Script.valid
active recognizer InvalidEmail: EmailAddress -> Choice<string,string>

Full name: Script.Email.( |ValidEmail|InvalidEmail| )
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
active recognizer ValidEmail: EmailAddress -> Choice<string,string>

Full name: Script.Email.( |ValidEmail|InvalidEmail| )
val valid : string
Raw view New version

More information

Link:http://fssnip.net/ma
Posted:5 years ago
Author:iceypoi
Tags: language