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.