2 ms·
No, functional languages also have generics. It is a type system feature, not related to OOP. Maybe the best example of a language without generics is C. In sh
by kaba0 4y ago
No, functional languages also have generics. It is a type system feature, not related to OOP.
Maybe the best example of a language without generics is C. In short there are 3 solutions to counter the lack of generics: copy-paste the same code multiple times with minor changes, make this copy-paste automatic with macros, or circumvent the problem by further abstraction.
Let’s say you want to create a vector data structure that can grow. It is not a hard thing if you know the size of each element, so you can trivially write it for, say, ints. Now you want to reuse your logic for your custom structs that have a different size - the only change you would have to do is replace the element_size constant and repeat the whole thing with a different name (first solution). You might try to write some ugly hack with C’s pre-processor macro to generate this thing for you by an easier to use wrapper, or you could just implement a wrapper that has a next, prev pointer and use a linked list structure (with very bad performance compared to storing the elements next to each other in an array). C code often uses the third option due to the inconvenience of the first 2, which would be solved by generics.
- MrYellowP 4y agoAahhh, now I get it! So you basically write code for "one size fits all" for the programmers convinience, at the cost of performance/general efficiency? Thank you for the response!