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

Type Equivalence: Structural vs Nominal

Comparing structural typing (based on type shape) with nominal typing (based on explicit type names and declarations).

Type Equivalence: Structural vs Nominal

What You'll Learn

You'll learn the two primary ways programming languages decide whether two types are "the same": by their structure or by their declared names.

The Core Idea

When a type checker needs to determine if two types are compatible, it must answer: "Are these two types equivalent?" Languages take one of two philosophical approaches:

Structural equivalence says two types are the same if they have the same shape—the same structure of fields, parameters, and return types, regardless of what they're called.

Nominal equivalence says two types are the same only if they have the same name or were declared to be related explicitly.

Structural Typing

In structural systems, if two record types both have an int field called x and a string field called y, they're considered equivalent—even if one is called Point and the other Coordinate. The compiler looks at the "blueprint" and ignores the label.

Analogy: Like judging two houses identical if they have the same floor plan, regardless of the street address.

Nominal Typing

In nominal systems, Point and Coordinate are different types even if structurally identical, because they were declared separately. You must explicitly state relationships (like inheritance or type aliases) for types to be equivalent.

Analogy: Like needing the same passport to enter a country—similar-looking documents from different nations don't count.

Why It Matters

  • Structural typing offers flexibility and duck typing (if it walks like a duck...), common in languages like TypeScript and OCaml
  • Nominal typing provides stronger guarantees and prevents accidental mixing of semantically different types (like mixing Money and Weight even if both are numbers), common in Java and C++

Key Takeaway: Structural equivalence checks type structure, while nominal equivalence checks type identity—each approach trades off flexibility for type safety guarantees.