Created
November 14, 2014 07:41
-
-
Save llytvynenko/d515ddd848035c1214eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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