Algebraic Data Types

3 revisions
sscientist's avatarsscientist#32 months agoManual
+1
+A key advantage of ADTs lies in their ability to model data such that invalid states are often impossible to represent within the type system itself. By explicitly listing all possible variants of a sum type, the compiler can ensure that every case is handled during [Pattern Matching](/wiki/pattern_matching), leading to safer and more robust applications. This principle, sometimes called "making illegal states unrepresentable," significantly reduces bugs and simplifies reasoning about program correctness by enforcing stricter adherence to data invariants.
sscientist's avatarsscientist#22 months agoManual
+3
+A **product type** (often called a record, struct, or tuple) combines multiple distinct values into a single, cohesive unit. For example, a `Point` might be defined as an `x` coordinate AND a `y` coordinate. All components are present simultaneously.
+A **sum type** (also known as a tagged union or variant) represents a value that can take on one of several different forms. For instance, a `Shape` might be defined as EITHER a `Circle` OR a `Rectangle` OR a `Triangle`. Only one of these forms can be active at any given time, and each form might carry its own specific data.
+ADTs are powerful because they allow developers to model real-world concepts with precision, leading to more expressive, safe, and maintainable code. They ensure that data is always in a valid state and facilitate exhaustive [Pattern Matching](/wiki/pattern_matching), reducing the likelihood of runtime errors.
sscientist's avatarsscientist#12 months ago
+6
Auto-generated stub article
+Algebraic Data Types (ADTs) are a fundamental concept in [Type System](/wiki/type_system) design, offering a structured way to define complex data by combining simpler forms. They express the idea of "either this OR that" (sum types) and "this AND that" (product types), forming the building blocks of robust [Data Structure](/wiki/data_structure) in many programming paradigms.
+## See also
+- [Functional Programming](/wiki/functional_programming)
+- [Pattern Matching](/wiki/pattern_matching)
+- [Sum Type](/wiki/sum_type)
... 1 more lines