4 ms·
Union, inheritance, dynamic_cast, enum class, std::variant... when all we needed all along was proper sum types.
by fouronnes3 2y ago
Union, inheritance, dynamic_cast, enum class, std::variant... when all we needed all along was proper sum types.
- searealist 2y agoSum types do not replace all the things you listed. Even Rust has different concepts for many of those.
- tialaramex 2y agoI don't think they're complaining that Sum types are equivalent to any or all of those things, but rather that what they wanted was Sum types and C++ has given them all this other stuff claiming it will solve their problem, instead of a Sum type which does solve the problem. If what I need is a bookshelf then it's true that a wardrobe, a cardboard box, a Kindle, and a shredder are all different things and a bookshelf would not substitute for any of them, but giving me the wardrobe, cardboard box, Kindle and shredder doesn't solve my problem, I needed a bookshelf.
- searealist 2y agoIt makes no sense to mention dyanmic_cast or inheritance (clearly referencing virtual functions here) when languages with sum types also have equivalents. It just shows a misunderstanding of how these features are used. I don't think I've ever seen an analogy contribute to a discussion, and yours is no exception.
- maattdd 2y agoInheritance (the subtyping part of it) is considered the OOP way to write sum type. sum Expr { Int; Add(Int,Int) } VS class Expr { } class Add extends Expr { Expr left; Expr right; }
- searealist 2y agoThat's not C++.
- maattdd 2y agoAny reader who comment here hopefully has enough knowledge to understand the implied C++. struct Add {} std::variant<Add, int> Expr; OR class Expr {} class Add : Expr {}
- searealist 2y agoI dont think you have ever seriously used c++.
- deleted 2y ago[deleted]
- gpderetta 2y agoThe expression problem enters the room.
- OvbiousError 2y agoYou should probably look up what a sum type is, it has nothing to do with summations. Your example doesn't contain a sum type. A C++ example: std:variant<int,std::string> sum_type_instance = 5;
- maattdd 2y agoMy sum type example is exactly this (but I didn't use C++ std::variant<> syntax to not confuse the reader). The most common example of a sum type is the "Expression problem" - please read some literature before commenting on a topic. (Btw, it's called sum type for a reason: summation. The cardinality of the sum type is the sum of the cardinality of its variants)
- Maxatar 2y agoYou seem to have a very narrow C++ view of what sum types are. Sum types are related to the expression problem, and the given example is the canonical instance of the expression problem. https://en.wikipedia.org/wiki/Expression_problem https://en.wikipedia.org/wiki/Expression_problem
- OvbiousError 2y agoIndeed. There seems to be quite the lack of understanding what the mentioned concepts actually are.
- tialaramex 2y agoMy guess currently is that C++ 29 will get some sort of pattern matching. It's too big a feature to board the C++ 26 train in autumn 2024 and there's nowhere close to consensus on how it should work or how it's spelled yet. So, assume C++ 29 has pattern matching. That causes people to attempt various idiomatic constructions from languages which always had pattern matching, and of course they're all rather awkward in C++. I think that realisation might point towards an actual language sum type, possibly via abortive attempts to "fix" matching and/or some of the pseudo-sum types provided by the C++ stdlib. So then you'd expect proposals in C++ 32 or C++ 35.
- gpderetta 2y agoThere are two competing pattern matching proposals, hopefully they'll converge soon.
- tialaramex 2y agoIn the last mailing Bjarne decided to offer his own syntax P3332, so that makes three.