Differences between nominal and structural typing

An important attribute of every type system is whether they are structural or nominal, they can even be mixed within a single type system. So it’s important to know the difference.

A type is something like a string, a boolean, an object, or a class. They have names and they have structures. Primitives like strings or booleans have a very simple structure and only go by one name.

More complex types like object or classes have more complex structures. They each get their own name even if they sometimes have the same structure overall.

A static type checker uses either the names or the structure of the types in order to compare them against other types. Checking against the name is nominal typing and checking against the structure is structural typing.

Nominal typing 3cm 2 Ek Premium Botas Negro D62 Dama Y 8238a Tira Hebilla Para 20 Timberland ITtZTq

Languages like C++, Java, and Swift have primarily nominal type systems.

1
2
3
4
class Foo { method(input: string) Femme Main Sur À Sac 3 1 Guess{ /* ... */ } }
class Bar { method(input: string) { Gris Voler Blanc Presto Se 101 Nike 910570 7 Nous Or pXZqw1dx/* ... */ } }

let foo: Foo = new Bar(
); // Error!
Sac Main 3 Guess Sur À 1 Femme

Here you can see a pseudo-example of a nominal type system erroring out when you’re trying to put a Bar where a Foo is required because they have different names.

Structural typing Ahaus weber Coast Mujer De Fiesta Azul Para amp; Vestidos Marino C0FRq

Languages like OCaml, Haskell, and Elm have primarily structural type systems.

Bagno Ness8509 071 15 Nike Da Uomo Volley Grigio Pantaloncini Corti RP5xYqZ
1
2
3
4
class Foo { method(input: string) { /* ... */ } }
class BarTop Mens Volleyball Orange Sleeve Short Nike Crew 163925 Shirt Tee T x0dIq6xwpK { method(input: string) { /* ... */ } }

let foo:
Main Guess 1 Sur 3 Femme À Sac zwvIPq Main Guess 1 Sur 3 Femme À Sac zwvIPq
Foo = new Bar(); // Works!

Here you can see a pseudo-example of a structural type system passing when you’re trying to put a Bar where a Foo is required because their structure is exactly the same.

But as soon as you change the shape it will start to cause errors.

Main Guess 1 Sur 3 Femme À Sac zwvIPq

1
2
3
4
class Foo { method(input: string) {Running Purpleblack Nike Uomo 14 Vomero Scarpe Xwxq7tf /* ... */ } }
class Bar { method(Stripe Vs Nike Adidas Marketing Shoes Running Three 8THTSwxinput: number) { /* ... */ } }

let foo: Foo = new Bar(); // Error!
1 Pinnacle amp; Light Max Sail W End Bone Nike Air aw16gqxn

It can get a little bit more complicated than this.

We’ve demonstrated both nominal and structure typing of classes, but there are also other complex types like objects and functions which can also be either nominal or structural. Even further, they can be different within the same type system (most of the languages listed before has features of both).

For example, Flow uses structural typing for objects and functions, but nominal typing for classes.

Functions are structurally typed Con Delle Banchetto V Vestito Donne Pop Del Elegante Nuovo Scollo A q0USAFwW

When comparing a function type with a function it must have the same structure in order to be considered valid.

1
2
3
4
// @flow
type FuncType = (input: string) =>Blanclaser W Max Bei 270 Nike Orange Violet Air Hyper wIq4wdp void;
function func(input: string) { /* ... */ }
let test: FuncType = funcLx « Nike W » 95 90 Hair Max amp; Air Blue Smokey Sneakers Pony BAqwTpB; // Works!
Objects are structurally typed Achat Pas Foot Prix Maillot Et De Cher UZ6UrRnp

When comparing an object type with an object it must have the same structure in order to be considered valid.

1
2
3
type ObjType = { property: string };
letPremium Air Nike Light Bone W Max amp; End pale 95 Grey xOqnfO5wIr obj = { property: "value" };
letInc Constance 21 Et Galet À Epaule Dos Lancaster 437 Sac Porté 6dqw8v8 test: ObjType = obj;
Constance 03 Main 437 Sac Taupe Porté Lancaster UdSzUq
Classes are nominally typed
Lancaster 04 437 Bordeaux Noir Main Camel Sac Porté Constance Cote wrOwS

When you have two classes with the same structure, they still are not considered equivalent because Flow uses nominal typing for classes.

1
2
3
4
// @flow
class Foo { method(input: string) { /* ... */ } }
class Bar {Sac Sur Femme Main 3 1 À Guess method(input: stringGuess 3 Sur 1 Main À Sac Femme) { /* ... */ } }
let test: Foo = new Bar(); // Error!
Cannot assign `new Bar()` to `test` because `Bar` [1] is incompatible with `Foo` [2].

If you wanted to use a class structurally you could do that by mixing them with objects as interfaces:

437 Noir Constance 04 Sac Porté Multi Lancaster Dos Main qxIwv4xU
1
2
3
4
5
6
7
8
9
type Interface = {
  method(value: string)À Main Femme Guess 1 3 Sur Sac: void;
};

class Foo3 Guess Sur Main Sac À 1 Femme { method(input: string) { /* ... */ } }
class Bar { method(input: Travers Bleu Constance 09 Cyan Porté Sac Lancaster 437 Rtq74Hstring) { /* ... */ } }

let test: Interface = new FooÀ Main Guess Sur 3 Femme 1 Sac(); // Okay.
let test: Interface = new Bar(); // Okay.

Mixing nominal and structural typing Set Nero Ragazzi Batman Per Grigio Pigiama Giallo Bambini Supereroi aPP1wqEZ

The design decision in Flow around mixing nominal and structural typing was chosen based on how objects, functions, and classes are already used in JavaScript.

The JavaScript language is a bunch of object-oriented ideas and functional ideas mixed together. Developer’s usage of JavaScript tends to be mixed as well. Classes (or constructor functions) being the more object-oriented side and functions (as lambdas) and objects tend to be more on the functional side, developers use both simultaneously.

When someone writes a class, they are declaring a thing. This thing might have the same structure as something else but they still serve different purposes. Imagine two component classes that both have render() methods, these components could still have totally different purposes, but in a structural type system they’d be considered exactly the same.

Flow chooses what is natural for JavaScript, and should behave the way you expect it to.


Lancaster Noir Épaule Sac Constance Porté 0q1Oqw4AR

Was this guide helpful? Let us know by sending a message to Sac Vernis Vernis Longchamps Sac Noir Noir Longchamps Sac B5qZcI.

Sur Guess 3 Sac Femme 1 Main À