Skip to content

Instantly share code, notes, and snippets.

@forki
Created June 21, 2012 09:32
Show Gist options
  • Save forki/2964839 to your computer and use it in GitHub Desktop.
Save forki/2964839 to your computer and use it in GitHub Desktop.
Static duck typing
let inline speak (a: ^a) =
let x = (^a : (member Name: string) (a))
printfn "I'm %s" x
let y = (^a : (member talk: unit -> string) (a))
printfn "I say %s" y
type Duck =
{ Name : string }
member x.talk() = "quackity quack"
type Dog(name) =
member x.Name = name
member x.talk() = "arrrr"
let x = { Name = "Donald" }
let y = new Dog("Pluto")
speak x
speak y
System.Console.ReadKey() |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment