Skip to content

Instantly share code, notes, and snippets.

@bleis-tift
Forked from forki/gist:2964839
Created June 22, 2012 00:32
Show Gist options
  • Save bleis-tift/2969500 to your computer and use it in GitHub Desktop.
Save bleis-tift/2969500 to your computer and use it in GitHub Desktop.
Static duck typing (F#2.0)
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 } // Name is not property in F# 2.0
//
// member x.talk() = "quackity quack"
type Dog(name: string) =
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