top | item 41157352

(no title)

maattdd | 1 year ago

Inheritance (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; }

discuss

order

OvbiousError|1 year ago

You 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|1 year ago

My 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)

gpderetta|1 year ago

The expression problem enters the room.

searealist|1 year ago

That's not C++.

maattdd|1 year ago

Any 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 {}