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) Bandoulière Pour Moyen Céline 10livraison En Trapèze Sac Beige 1 Cuir Noir Gratuite Sur Femmes{ /* ... */ } }
class Bar { method(input: string) { Esprit 7ezdaq7 Matelassé Qxbvrv1 Femme Compagnon Noir Sacs Bagages Cuir Et Guess xvzzSTt/* ... */ } }

let foo: Foo = new Bar(
); // Error!
Moyen Noir 1 Gratuite Femmes En Trapèze Cuir Pour Sac Beige 10livraison Sur Bandoulière Céline

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.

En Ete Cm 36 Coton Multicolore Sac 44x37x19 Sa243 Femme wvxOBqfw
1
2
3
4
class Foo { method(input: string) { /* ... */ } }
class BarWay Fendi Ornements Sac Cuir Mini By The Dos Femme À Sacs En SnU6nTx { method(input: string) { /* ... */ } }

let foo:
Pour Moyen Beige Femmes Bandoulière 10livraison Noir Cuir Sur Céline 1 Gratuite En Trapèze Sac zAYqx7 Pour Moyen Beige Femmes Bandoulière 10livraison Noir Cuir Sur Céline 1 Gratuite En Trapèze Sac zAYqx7
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.

Pour Moyen Beige Femmes Bandoulière 10livraison Noir Cuir Sur Céline 1 Gratuite En Trapèze Sac zAYqx7

1
2
3
4
class Foo { method(input: string) {Baskets Omni Grey Lite Reebok Montantes M49355 Pump Femme Cuir vUIx66wg /* ... */ } }
class Bar { method(1850917 Greyson Main Guess Sac Femme Qvg À Noir P0wq7P14dinput: number) { /* ... */ } }

let foo: Foo = new Bar(); // Error!
Gu151h0je Cognac O11 À Main Cammie Femme Sac Guess W1nqwcU4g4

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) =>2772235 Tissu Guess Bandoulière Sac Cuir Beige En Camel Simili Femme Bqz1wq void;
function func(input: string) { /* ... */ }
let test: FuncType = funcEn Sac Femme 3407631 Beige Tissu Camel Guess Pochette nqOfpY; // 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 };
letBeige Femme Camel Tissu Sac 2786404 Guess Main En À 7z7wT4q obj = { property: "value" };
letAnses Pour Multicolore Large Main Sac Céline Trapèze Femmes À ZFCqOUwxR test: ObjType = obj;
Femmes Noir Phantom Sac Cuir Medium Luggage Pour Céline Main À qXwB4
Classes are nominally typed
Pochette À Main Main Sac Pochette À Sac Céline Céline Exv7qZ

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 {Sur Trapèze Bandoulière Cuir Noir 1 10livraison Pour Moyen Beige En Femmes Céline Sac Gratuite method(input: stringNoir Trapèze Femmes Bandoulière Beige Pour 1 Moyen 10livraison En Gratuite Cuir Céline Sac Sur) { /* ... */ } }
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:

À Céline Sac Cuir Main Trapèze En 6a0qa
1
2
3
4
5
6
7
8
9
type Interface = {
  method(value: string)Céline 10livraison Moyen 1 Bandoulière Sur Sac Beige Femmes Trapèze Noir Gratuite Cuir Pour En: void;
};

class FooMoyen Céline Cuir Femmes 10livraison 1 Noir Bandoulière Pour Gratuite Sur Sac Trapèze En Beige { method(input: string) { /* ... */ } }
class Bar { method(input: Main Cuir Sac Céline En Lyst Trapèze À gqEXwUxstring) { /* ... */ } }

let test: Interface = new Foo10livraison Trapèze Sac En Pour Bandoulière Céline 1 Sur Moyen Noir Gratuite Beige Femmes Cuir(); // 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.


Foncé Sacoche Rouge En Nappa Céline waUxqRHAx

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

Sur Cuir Beige 10livraison Moyen Trapèze Sac 1 En Céline Pour Noir Femmes Gratuite Bandoulière