algebraic data types
A common language feature in functional programming languages that allows easily declaring types that are a sum of products.
Examples in various languages:
data Stuff = Foo {_a :: Int, _b :: Bool} | Bar (Maybe Foo)
(* no guarantee this works; I barely remembered OCaml when I wrote this *)
type stuff = foo of (int, bool) | bar of (foo option)
enum Stuff {
case foo(a: Int, b: Bool)
case bar(Foo?)
}
```rust
// same as the OCaml, still good enough to demonstrate basic syntax
enum stuff {
Foo(i32, bool),
Bar(Box<stuff>)
}
Algebraic data types tend to make it easy to describe data that might come in several different shapes, and also are very good at representing tree like structures.
Essentially they’re a single feature bundling together:
Common object oriented programming languages use a lot of boilerplate when representing ADTs because they represent these as class hierarchies.