4 ms·
Ironically generics don't necessarily solve this. C# requires multiple implementations of numeric methods because int/float/etc don't sit in a type hierarchy.
by cdoxsey 8y ago
Ironically generics don't necessarily solve this. C# requires multiple implementations of numeric methods because int/float/etc don't sit in a type hierarchy.
It also doesn't really work in Java either because you have to use the Object types.
Even in rust this looks pretty complicated: https://travisf.net/rust-generic-numbers https://travisf.net/rust-generic-numbers
Maybe it's easy in c++?
I've seem a similar comment made on almost every discussion of this issue... are folks just not aware of the limitations with generic implementations?
- annywhey 8y agoIn agreement. IMHO generics basically have one place where they work well: abstract containers. Your queues, linked lists, what-have-you can benefit. But most other data structures? Nope. Too much of the algorithm depends on the primitive type. And that makes generics a nice boon for computer scientists and certain library authors, but of middling benefit for day-to-day coding. If the container works, you can copy-paste-modify your way to the types you want, or automate the same in a macro or code generator. It's not beautiful but it doesn't have to be.
- kungito 8y ago"Generics" (which should allow higher kinded types) work well with monads, who containers are among many others.
- int_19h 8y agoHaving coded C# for many years now, generics are used for a lot more than just containers. There's a great many things that can be generalized. Just look at any large C# project on GitHub.
- mseepgood 8y agoIt would be easy with the proposed Go 2 contracts.
- civility 8y agoYes, it works well enough in C++. It would be nice if another language, without C++'s other pitfalls, got some of this right. Btw, it's "ironic" that you think pointing at two broken implementations says anything about where it works. "Are folks just not aware" it's ridiculous to be snarky when arguing from a position of ignorance?
- slavik81 8y agoThe complexity in that rust example comes from the indirection of requiring you to declare what operations you require via traits. C++ is much simpler: whatever operation you use is required.