liveBook · Manning

Product type

As explained in Data-Oriented Programming in Java

A product type is a type whose values combine one value from each of several component types, corresponding to logical AND and a Cartesian product.

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

How Data-Oriented Programming in Java covers it

How deep it goes
Defines products through Cartesian products, then previews symbolic laws and exhaustive state generation.
Examples
Uses a Beverage record and exhaustive RawData state generation with optional fields.
What it assumes
Assumes Java records and introduces the set-of-values interpretation needed for product types.
What you can do after
Evaluate/understand a record’s state space and use it for exhaustive invariant assertions.
How it connects
Connects records, algebraic data types, sum types, sealed interfaces, Optional, invariants, and Cartesian products.

Also explained in

Compare how 3 books explain product 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.