This essay says that inheritance is harmful and if possible you should “ban inheritance completely”. You see these arguments a lot, as well as things like “prefer composition to inheritance”. A lot of these arguments argue that in practice inheritance has problems. But they don’t preclude inheritance working in another context, maybe with a better language syntax. And it doesn’t explain why inheritance became so popular in the first place. I want to explore what’s fundamentally challenging about inheritance and why we all use it anyway.
Saying “X is bad” or “Y over X” is not the same as saying “there is never a place for X”. I think JS is a pretty bad language, and prefer other languages to it, but I still recognise very obvious places where it should be used.
Maybe it depends on the way you understand types, but to me sum and product types are completely intuitive. A type is a set of possible values. A sum type is multiple sets added together (summed).
That makes sense for
str | int
, but how is an enum a “sum type”?As for product types, in set theory a product of sets is a cartesian product. How is a
a product? What is it a product of? And why is the type itself a product, not
Dog x Cat
? Or isDog x Cat
indeed some kind of product that I’m not aware of but with another syntax?Anti Commercial-AI license
Well what is an enum except a chain of
X | Y | Z | ...
. An enum can be any of its variants, and therefore the set of its possible values are just all possibilities of its variants added together.Consider this enum:
enum Foo { A, B(bool), }
The possible values for
A
are just one:A
. The possible values forB
areB( true )
andB( false )
. So the total possible values forFoo
are simply these sets combined:A
orB( true )
orB( false )
.As for product types, what it is the product is, is still the same: the sets of possible values. Consider the possible values for the product of
A
andB
. For every possible value ofA
, a value could be made by matching it with any possible value ofB
(so, multiplication). If there are 3 possible values ofA
, and two possible values ofB
, then the total number of possible combinations of these for the product type is 6.In your example,
Dog
is a product ofu8
, anotheru8
, andString
. If you decide to add a Boolean field to this type, accordingly the size of the set of options would double, because for every possibleDog
you currently have, two possibilities would be created, one with atrue
and one with afalse
.As for your last question, some languages might use
x
as a product type syntax, but because tuples and structs are inherently product types, most languages use those as Syntax. For example in Haskell the product type ofDog
andCat
would be written as(Dog, Cat)
.That rarely comes across online where opinions are often stated dichotomously. Especially when speaking of inheritance, some crowds (I’ve noticed this in the Rust crowd) are vehemently against it and will do nigh anything not to think of a problem as one of inheritance nor a solution that could benefit from inheritance. The makers of the servo browser engine which has to implement hierarchical structures (the DOM) ran against this exact issue within the Rust community where inheritance might as well equate to blasphemy.
I recognise that it’s probably a loud, zealous minority, but it makes interacting with the rest of the community rather difficult.
Anti Commercial-AI license