liveBook · Manning

Algebraic data type

As explained in Data-Oriented Programming in Java

An algebraic data type is a composite type whose values are formed by combining product and sum alternatives of other types.

A domain model becomes muddy when one record carries fields whose meanings conflict, or when flags and nulls quietly encode mutually exclusive states. Algebraic data types make those choices explicit: combine facts that coexist with AND, and model alternatives that exclude one another with OR. In Java, records express the AND shape and sealed interfaces express the OR shape. section 4.1 section 4.9

A product type combines components with ×. A drink that is both a particular Drink and a Served state has the shape Drink × Served: every value contains both parts. A sum type combines alternatives with +. Coffee + Tea + Milk means a value is one of those choices. Sealing makes the boundary explicit: these options, and no others. The point is semantic precision, not replacing object-oriented design wholesale. section 4.6 section 4.9 section 4.7

Turn a runtime branch into domain data

Instead of making a decision disappear inside if/else code, return a sealed decision type such as BillableFee OR NotBillable OR NeedsApproval. Actions can then be deferred and interpreted separately: bill a billable fee, start approval, or preserve a rejected result. Making each branch a type exposes missing lifecycle states and lets the data model tell the process story before implementation details are fixed. section 5.3.2

The algebra also provides a compact sketching language. Use symbolic names such as A and B to reason about model shapes, then translate them into Java. Product and sum operations follow familiar algebraic laws, including commutation and distribution; optionality can be represented as a choice between a one-value empty case and a value of type A. These transformations can reveal duplicated structure before records and interfaces are refactored. section 4.6.3 section 4.6.4

Figure 4.19 Applying the distributive law over Sum and Product types to factor out duplication

Follow the distributive-law refactoring to see how algebraic notation identifies duplicated alternatives that can be factored into a clearer model. section 4.6.4

How Data-Oriented Programming in Java covers it

How deep it goes
Explains product and sum operators, symbolic laws, optionality, and a preview of algebraic refactoring; defers detailed practice.
Examples
Uses checklists, Coffee/Tea/Milk drinks, Optional, and late-fee billing decisions to show type combinations and alternatives.
What it assumes
Assumes Java familiarity while building the connection from records and sealed interfaces to algebraic data types.
What you can do after
Evaluate/understand domain models as explicit AND/OR type shapes and sketch refactorings before committing to implementations.
How it connects
Connects records, sealed interfaces, product types, sum types, optionality, pattern matching, and runtime decisions.

Also explained in

Compare how 6 books explain algebraic data type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.