4 ms·
Generics are highly reusable is true. But people don't read and write them often because of the language design. Most Hindley Milner typed languages like ML a
by namelosw 7y ago
Generics are highly reusable is true.
But people don't read and write them often because of the language design.
Most Hindley Milner typed languages like ML and Haskell by default are generic by default and it's not harder or more complex than non-generic code.
Thinking of a function 'swap'. The signature should just be swap : (a, b) -> (b, a) instead of swap: <T, U>(x: (T, U)) => (U, T). The language design in most of languages which discourage people using generics.
In fact, non-generic code usually introduce couplings to the argument type, making things harder to change, and violates the least power principle.
- Faark 7y agoThis works nice for something as simple as a swap. But more complicated use-cases have requirements. I want those explicitly stated rather than inferred by a compiler. Abstracting the actually used features behind names like U: String, T: Map is can keep complexity hidden. Or would you prefer the compiler to auto-generate them for you from usage? (like U: must have ChatAt(int)->Char, Length->int; T: must have put(U), get(U), Length->Int) I certainly prefer names. And those have to be declared somewhere.