Let's say I defined in F# the following two types:
type Dog = { DogName:string; Age:int }
type Cat = { CatName:string; Age:int }
I was expecting the following method to work for both cats and dogs:
let isOld x = x.Age >= 65
Actually, what seems to happen is that isOld
will only accept cats:
let dog = { DogName = "Jackie"; Age = 4 }
let cat = { CatName = "Micky"; Age = 80 }
let isDogOld = isOld dog //error
My hopes were that F# would be smart enough to define some kind of "virtual" interface X
for both cats and dogs so that isOld
would accept a X as argument, instead of a Cat
.
This isn't something that F# will in any circumstance handle, am I right? It seems like F# type inference system would not do anything more than what the C# does with var
typed variables.
Usually what is meant by F# duck-typing is a compile-time polymorphism. Syntax is a little weirder, but you should be able to work it out from the following example -
Remember, this polymorphism only works at compile-time!
You can define an
inline
function with a member constraint, or go the classic route and use an interface (which would probably be preferred in this case).EDIT
I just remembered this won't work for record types. Technically their members are fields, although you can amend them with members using
with member ...
. You would have to do that to satisfy an interface anyway.For reference, here's how you would implement an interface with a record type:
FSharp.Interop.Dynamic (on nuget) provides a DLR based implementation of the dynamic operator (real dynamic duck typing)