2 ms·
F# uses nominal typing - that's another difference between it and Caml. That said, F# will let you do the things you listed - one can be handled with tradition
by bunderbunder 13y ago
F# uses nominal typing - that's another difference between it and Caml.
That said, F# will let you do the things you listed - one can be handled with traditional OOP techniques, and the other is handled by discriminated unions. Structural typing is a bit different from what you're describing - it's more a kind of compile-time duck typing.
I actually think F#'s switch to nominative typing is a big improvement over its Caml roots. When I'm trying to build out a type system that's meant to use type checks to enforce sanity, I want to have explicit control over when and where types are considered to be equivalent. Just because the types foo and bar implement members with similar signatures does not mean that they carry the same semantics.
Therefore, IMO having the language try to be clever and jump to the conclusion that anything that looks like a duck must be a duck is a hindrance to type safety. It's also a bit of a maintainability hassle: Having a formal type definition for each equivalence class makes it easier to find which types need to be modified accordingly when an equivalence class's definition changes.