Dynamic, structured, inferred typing: what's the difference?

Types of typing

Typing Meaning Example
Static Types are declared upfront and variables may not change types int x = 5;
Dynamic Types may change at runtime let x = 5; x = "Hello!"
Structured Types with the same structure are interchangeable Shown below
Nominal Types with different names are not interchangeable Shown below
Manifest Variable types are explicitly declared int x = 5;
Inferred Variable types are determined by the compiler auto x = 5;

Structured typing example (Javascript)

function Human() {
  this.name = "Mieszko";
}

function Vehicle() {
  this.name = "Rolls Royce";
}

// {mieszko} and {royce} can be used interchangeably
let mieszko = new Human();
let royce = new Vehicle();

function printName(human) {
  console.log(human.name);
}

printName(mieszko); // Fine
printName(royce); // Fine

Nominal typing example (Java)

class Human {
  String name = "Mieszko";
}

class Vehicle {
  String name = "Rolls Royce";
}

// {mieszko} and {royce} cannot be used interchangeably
Human mieszko = new Human();
Vehicle royce = new Vehicle();

void printName(Human human) {
  System.out.println(human.name);
}

printName(mieszko); // Fine
printName(royce); // Does not compile

Static typing results in fewer bugs

The paper To Type or Not to Type: Quantifying Detectable Bugs in JavaScript explores the relationship between static typing and the detection of type related errors.

Dynamic typing is simple to understand and its flexibility allows for more compact code. However, the paper finds that type systems like Typescript and Flow result in 15% fewer bugs in open source projects.

More

Find the closest sum to s
This is the solution for a difficult problem in ECS529U Algorithms and Data Structures (Queen Mary University of London), Lab 10. Try to…
Building safe atomic transactions with Deno KV
The Deno Team are building a unique product. It's an all-javascript development experience with _very_ simple deployments: Deno KV is a…
Social media image previews with Deno Fresh
When you share a link on Twitter, Facebook, LinkedIn or Discord, you can see an image with a preview of the page you're linking to. This…
See more articles
Email