Remix.run Logo
munchler 5 hours ago

Unions are simpler than subclasses and more powerful than enums, so the use cases are plentiful. This should reduce the proliferation of verbose class hierarchies in C#. Algebraic data types (i.e. records and unions) can usually express domain models much more succinctly than traditional OO.

Quarrelsome 5 hours ago | parent [-]

> so the use cases are plentiful

such as?

> This should reduce the proliferation of verbose class hierarchies in C#

So just as an alternative for class hierarchies? I mean good people already balance that by having a preference for composition.

munchler 5 hours ago | parent | next [-]

Simple example:

   type Expr =
       | Primitive of int
       | Addition of (Expr * Expr)
       | Subtraction of (Expr * Expr)
       | Negation of Expr
Quarrelsome 5 hours ago | parent [-]

Isn't that just Func<int> ?

afdbcreid 4 hours ago | parent | next [-]

Really not. You can, of course, having instead a delegate to evaluate the expression. But then that's all you can do. You can't pretty-print it, for example, or optimize it, or whatever.

4 hours ago | parent | prev [-]
[deleted]
LeFantome 4 hours ago | parent | prev [-]

“Compoision”. A typo I know but it would be a word describing what goes wrong with class hierarchies.