-
-
Save bleis-tift/2969500 to your computer and use it in GitHub Desktop.
Static duck typing (F#2.0)
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
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