4 ms·
> How else do you merge two structs if not using inheritance? By merging them. Structs are product types. If you merge them, you get a bigger product type. You
by dgb23 1y ago
> How else do you merge two structs if not using inheritance?
By merging them. Structs are product types. If you merge them, you get a bigger product type. You don't need inheritance (ADTs) for that.
The more useful point of inheritance is having shared commonality. But modern languages make it convenient to express that without using ADTs/inheritance.
TypeScript is fully structurally typed. If you combine a Foo and a Bar it is something new, but keeps being both a Foo and a Bar as well.
Go is structurally typed to a relatively high degree as well. You can embed types (including structs) into structs and only care about the individual parts in your functions. And you have composable and implicit interfaces.
Clojure has protocols and generally only cares about the things you use or define to use in functions. It allows you to do hierarchical keyword ontologies if you want, but I see it rarely used.
These languages and many others favor two fundamental building blocks: composition and signatures. The latter being either about data fields or function signatures. The neat part is these aren't entangled: You can use and talk about them separately.
How fundamental is inheritance if it can be fully replaced by simpler building blocks?
- ninetyninenine 1y ago>By merging them. Structs are product types. If you merge them, you get a bigger product type. You don't need inheritance (ADTs) for that. Merging structs and inheritance are fundamentally the same thing. >How fundamental is inheritance if it can be fully replaced by simpler building blocks? It can't be replaced. Combining Foo and Bar in the way you're thinking involves additional primitives and concepts like nesting. If Foo and Bar share a same property the most straight forward way of handling is overriding one property with the other. Overriding IS inheritance. We aren't dealing with product types in the purest form either. These product types have named properties and you need additional rules to handle conflicting names. In fact once you have named properties the resulting algebra from multiplying structs is not consistent with the concept of multiplication whether you use inheritance or "object composition"