3 ms·
You want some way of specifying an interface - that doesn't necessarily imply classes. Also, class-based inheritance tends to be a poor fit for interfaces; not
by emn13 13y ago
You want some way of specifying an interface - that doesn't necessarily imply classes. Also, class-based inheritance tends to be a poor fit for interfaces; note for example that C#/Java have their own special-purpose interface mechanics, and even C++ which allows multiple inheritance tends to use implicit template-based interface implementation instead.
Discriminated unions on the other hand are - the name says it - intended for cases where you want to discriminate between multiple possible values. They're by design not intended to hide differences behind an interface; they don't address the same problem at all.
All in all, I think that neither classes nor discriminated unions solve the interface problem (nor were they ever really designed to do so) - that's a different problem.
- pjmlp 13y agoYou can have OO without classes. I just meant OO in general, not classes specifically.
- emn13 13y agoYou can have Objects without classes, certainly - even C had structs, and all functional languages I know of have records or something similar. It's a question of terminology whether you call that object-oriented. It's certainly part of OO, in any case.