A product type combines components that must all be present: A AND B. In Java, a record is the everyday form. `record Beverage(Drinks drink, Served served) {}` combines a drink choice with a serving choice, making each valid pairing a value of `Beverage`. section 4.6.1
Table 4.1 comparing creating a new type with records with creating a new type with products
|
Java Record
|
Algebraic
Definition
|
|
enum Drinks {Coffee, Tea};
enum Served {Hot, Cold};
record Beverage(
Drinks drink,
Served served
){}
|
Beverage =
Drinks × Served
|
Compare record syntax with the compact algebraic notation `Drinks × Served`. section 4.6.1
The name comes from the values a type can hold. If `Drinks` has Coffee and Tea, and `Served` has Hot and Cold, the product type contains their Cartesian product: Coffee/Hot, Tea/Hot, Coffee/Cold, and Tea/Cold. Adding an attribute multiplies the number of expressible states, so an innocent-looking record can also admit combinations with conflicting domain meanings. section 4.6.1 section 4.9
Count states before trusting a record
A composite record’s possible states can be generated by taking every value of each attribute and nesting the choices. Optional attributes add their empty case. That complete set can drive assertions that an invariant holds for every possible record, rather than only for selected examples. section 7.5
Product notation is also a design sketching tool. Write `Drink × Served` to expose what is combined before committing to Java syntax. Products commute, and they distribute across sum types, so symbolic models can be rearranged to factor duplication before translating them back into records and sealed interfaces. section 4.6.3 section 4.6.4


