3 ms·
> the code for linking e.g. nodes in a tree should be the same regardless of the data structure where the headers are embedded. Why should that be so? It's par
by tsimionescu 5y ago
> the code for linking e.g. nodes in a tree should be the same regardless of the data structure where the headers are embedded.
Why should that be so? It's particularly NOT true for one of the simplest and most efficient data structures in any program: the array. The code that needs to work with an array needs to know exactly the size of each element of the array, so you basically can't have an efficient language that doesn't support generic arrays. Haskell maybe sometimes gets by with a very smart compiler and heavy use of laziness, but even C has support for generic arrays.
- deleted 5y ago[deleted]
- jstimpfle 5y agoYou can totally make array iteration generic, just pass the element size. Not saying it is necessarily a good idea. I agree that there are cases where the "there should be a single central piece of code" argument is not a strong one, because the generic code is so simple that it should be inlined into the usage code. Arrays iteration is one example - but note that for example sorting code is, while the sorting algorithm might be generic, more than just generic array iteration. Doubly linked lists already profit from shared code in some ways, and definitely I'd find it a bad idea to replicate for example tree mutation code (RB tree or similar) for each node type. Embedding the header structure and keeping the links just between these embedded headers, to be able to reuse a common (compiled) piece of code is a very good idea here IMO.