Records in Kotlin: Named Product Types
In Kotlin, data classes provide a powerful and idiomatic way to model named product types — a concept deeply rooted in functional programming and category theory. Understanding how these types work is essential for writing clean, maintainable, and expressive code.
What is a Product Type?
A product type is a type that combines multiple values into a single composite value. All components of the product type exist together — they are not alternatives, but rather parts of the same whole.
For example, a Car can be described by its model, manufacturer, and year. These are not separate entities, but they are all required for a complete representation of a car. This is a classic example of a product type.
In terms of structure, a product type can be thought of as:
String × String × IntThis represents a combination of a string (model), another string (manufacturer), and an integer (year). All components are required, and none are optional.
Records: Named Product Types
A record is a product type where each component has a name rather than being identified by position. This is a key distinction between records and tuples, which are positional in nature.
For example, the following is a tuple:
Triple("Swift", "Maruti", 2024)Here, the meaning of each value is determined by its position in the tuple. This can make the code harder to read and maintain, especially as the number of components increases.
In contrast, a record provides clarity by assigning meaningful names to each component:
data class Car(
val model: String,
val manufacturer: String,
val year: Int
)This makes the code self-explanatory and reduces the risk of errors due to misinterpretation of position.
Kotlin Data Classes: Practical Named Product Types
Kotlin’s data class is a practical and idiomatic way to implement named product types. It automatically provides useful features like toString(), equals(), and copy(), making it ideal for modeling data structures.
Here’s how you might create an instance of a Car:
val car = Car(
model = "Creta",
manufacturer = "Hyundai",
year = 2025
)This is far more readable than using a Triple, and it clearly communicates the intent of the code.
Nested Product Types
Product types can also contain other product types. For example, a Car might have an Engine:
data class Engine(
val horsepower: Int,
val fuelType: String
)
data class Car(
val model: String,
val manufacturer: String,
val year: Int,
val engine: Engine
)This structure is conceptually represented as:
Car
├── String
├── String
├── Int
└── Engine
├── Int
└── StringThis nesting of product types is a common and powerful technique in functional programming.
Product vs Coproduct
While product types represent the idea of AND (all components exist together), coproduct types represent the idea of OR (a value is one of several alternatives).
For example, a Vehicle can be either a Car or a Bike:
sealed interface Vehicle
data class Car(val model: String) : Vehicle
data class Bike(val model: String) : VehicleThis is a coproduct type, and it represents the logical OR between Car and Bike.
The Category Theory Connection
From a theoretical perspective, product types are a foundational concept in category theory. A product combines multiple objects into one:
A × BIn the case of a Car, this becomes:
String × String × IntBy giving this product a meaningful name like Car, we are essentially naming a domain-specific structure that is isomorphic to the product type.
This is not just a syntactic convenience — it’s a semantic and structural equivalence.
Final Mental Model
When you see this Kotlin code:
data class Car(
val model: String,
val manufacturer: String,
val year: Int
)Think of it as:
Car
↓
A named product type
↓
String × String × Int
↓
All values exist together
↓
Each value has a meaningful nameThis is the core idea behind records as product types — a concept that is both practical and deeply rooted in theoretical computer science.
Conclusion
Understanding records as named product types is essential for writing clean, expressive, and maintainable code in Kotlin. By using data classes, you are not just modeling data — you're modeling meaningful domain structures that are both readable and powerful.
This understanding is not only useful in Kotlin, but also in the broader context of functional programming and category theory, where product types are a fundamental building block.
