Created
September 12, 2013 07:09
-
-
Save ssboisen/6533869 to your computer and use it in GitHub Desktop.
Static duck-typing in F#
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 inTheForest (a: ^a) = | |
(^a : (member Quack: unit -> unit) (a)) | |
(^a : (member Feathers: unit -> unit) (a)) | |
() | |
type Duck() = | |
member x.Quack() = printfn "Quaaaaaack!" | |
member x.Feathers() = printfn "The duck has white and gray feathers." | |
type Person() = | |
member x.Quack() = printfn "The person imitates a duck." | |
member x.Feathers() = printfn "The person takes a feather from the ground and shows it." | |
type Dove() = | |
member x.Feathers() = printfn "The pidgin has white feathers" | |
inTheForest(new Duck()) | |
inTheForest(new Person()) | |
inTheForest(new Dove()) //Compile error: The type 'Dove' does not support the operator 'Quack'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment