4 ms·
C++ is very, very hurt by not having a base Object type from which all other types derive. Because of this, we get templates, and templates suck a lot. Generic
by moron4hire 11y ago
C++ is very, very hurt by not having a base Object type from which all other types derive. Because of this, we get templates, and templates suck a lot.
Generics in Java are even worse. All of the idiocy of C++ templates + Java type-erased containers, none of the benefits. I've been bit way too many times by Java generics just forgetting what type they contain.
Generics in C# are slightly better, especially in that there are a lot of different types of constraints you can apply to them. When applied judiciously, they can greatly simplify a lot of code. They are work best when everything is specified "correctly" with interfaces. Hold on a sec...
Sometimes I think templates/generics is all just a hack to get around the fact that I can't add interfaces to types I didn't define myself. When I'm designing a wholly closed system, I'm often going the old-old Java way of explicit interfaces for everything. It just keeps you honest about state encapsulation, which then keeps you honest about state transitions, which then basically just makes the program write itself.
So if you have a system like that, C# generics and its constraints work really well. But probably the entire reason you wanted generics in the first place was to not have to write all those interfaces. And you still can't add interfaces to things that you didn't write.
But at least you can write extension methods for them.
Of course, you can't get all the way to an algebraic type system with it. It makes you think you can. But you can't. That hurts.
- nly 11y ago> Sometimes I think templates/generics is all just a hack to get around the fact that I can't add interfaces to types I didn't define myself You can though. They're called free functions. Types should only expose their basis functions[0] as members, keeping their surface small. Once you have that small set of reasonable functions, you can write free functions to do everything else. Sometimes if several of your operand types share a minimal set of basis functions it's useful to make these algorithms templates. That's all. Scott Meyers was writing about this stuff 20 years ago. Here's a 15 year old example[1]. Here's another, more recent, article[1] by Walter Bright, which compares the approaches of C#, C++ and D. This is the reason why C++ has the 'madness', as some people see it, of function overloading and Argument Dependent Lookup[3] and, soon, a Unified Call Syntax of its own. So you want all classes derived from an Object base. Fine. What are you basis functions for Object? [0] https://en.wikipedia.org/wiki/Basis_function https://en.wikipedia.org/wiki/Basis_function [1] http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197?pgno=3 http://www.drdobbs.com/cpp/how-non-member-functions-improve-... [2] http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394 http://www.drdobbs.com/cpp/uniform-function-call-syntax/2327... [3] http://en.cppreference.com/w/cpp/language/adl http://en.cppreference.com/w/cpp/language/adl
- MichaelBurge 11y agoWhen I used to use OOP, I would use void* as a poor man's base object. It's harder to get that with non-pointers(or pointer-equivalents, like references): What is the calling convention for functions that take Object and return Object? Realize that somebody could upcast an integer into Object and then downcast into a 10000 byte struct, and this could happen across different object files that were written in different languages.