by maattdd · 2y ago · 0 comments
sum Expr { Int; Add(Int,Int) }
VS
class Expr { }
class Add extends Expr { Expr left; Expr right; }
std:variant<int,std::string> sum_type_instance = 5;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)
Tagged unions are an implementation and one that's often a poor fit for the problem. Sum types are an idea from type theory, rather than an implementation detail. It's very on-brand for C++ to have standardized a poor implementation detail rather than the useful idea.
Look at how much hoop jumping was required to make std::optional<T&> work for C++ 26 and then compare how Rust's Option<&T> isn't even special, that's just naturally what happens.
struct Add {} std::variant<Add, int> Expr;
OR
class Expr {} class Add : Expr {}