Skip to content

Instantly share code, notes, and snippets.

@llytvynenko
Created November 14, 2014 07:41
Show Gist options
  • Save llytvynenko/d515ddd848035c1214eb to your computer and use it in GitHub Desktop.
Save llytvynenko/d515ddd848035c1214eb to your computer and use it in GitHub Desktop.
// typeclass
type Showable<'a> = { show : 'a -> unit; showPretty : 'a -> unit } //'
// instances
let IntShowable =
{ show = printfn "%d"; showPretty = (fun i -> printfn "pretty %d" i) }
let StringShowable =
{ show = printfn "%s"; showPretty = (fun s -> printfn "<<%s>>" s) }
// function using typeclass constraint
// Showable a => [a] -> ()
let ShowAllPretty (s:Showable<'a>) l = //'
l |> List.iter s.showPretty
// callsites
ShowAllPretty IntShowable [1;2;3]
ShowAllPretty StringShowable ["foo";"bar"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment