Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Static duck-typing in F#
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
You can’t perform that action at this time.