Course contentsShow
← Computer Science
Lesson 1987 of 2,87245. Type Systems and SemanticsFree lesson

Composite Types: Products

Understanding product types (tuples, structs, records) that combine multiple values into a single compound type.

Composite Types: Products

What You'll Learn

You'll learn how product types bundle multiple values—potentially of different types—into a single composite structure.

The Core Idea

After understanding primitive types (integers, booleans, characters), we need ways to group multiple values together. Product types are the simplest composite types: they combine two or more values into one unit, creating a Cartesian product of their component types.

Think of a product type like a filing folder that holds several distinct pieces of paper—each paper (field) can be a different type, but they're all kept together as one package.

Common Forms

Tuples are the simplest products: ordered collections of heterogeneous values. A tuple (Int, Bool, String) holds exactly three values of those specific types, accessible by position.

Structs or records are named products. Instead of accessing by position, each component has a field name. A Person record might have fields name: String, age: Int, and isStudent: Bool.

Why "Product"? The term comes from set theory. If type A has m possible values and type B has n possible values, the product type (A, B) has m × n possible combinations. A (Bool, Bool) pair has 2 × 2 = 4 possibilities: (true, true), (true, false), (false, true), (false, false).

In ASTs

When building Abstract Syntax Trees, product types naturally represent nodes that contain multiple pieces of data—like a function definition node holding a name, parameter list, return type, and body.

Key Takeaway: Product types combine multiple values (of the same or different types) into a single compound value, letting you treat related data as one unit while maintaining type safety for each component.