• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Type Systems

Contents

  • Dynamic vs Static Typing
  • Strong vs Weak Typing
  • Explicit vs Implicit Typing
  • Overview
  • See also

The topic of type system keeps comming up when I hear people discuss programming languages. So let's summarize it.

Dynamic vs Static Typing

Rule of thumb: Statically typed languages check their types at compile time, while dynamically typed languages check their types at runtime.

I like the definition of pythonconquerstheuniverse:

In a statically typed language, every variable name is bound both a type (at compile time, by means of a data declaration) and an object.

In a dynamically typed language, every variable name is bound only to an object.

Something you can do in a dynamic language, but not in a static language:

foo = "bar"
foo = 9

So the model you have in mind is different. In statically typed languages you imagine variables as containers for specific types of information. In dynamically typed languages, you imagine variables as post-it notes.

The advantage of statically typed languages is less confusion.

Strong vs Weak Typing

Rule of thumb: Weakly typed languages do type coercion.

Continuing with the definition of pythonconquerstheuniverse:

In a weakly typed language, variables can be implicitly coerced to unrelated types, whereas in a strongly typed language they cannot, and an explicit conversion is required.

What you can do in weakly typed languages, but not in strongly typed languages:

foo = "hello"
bar = 1337
foo + bar

Instead, the strongly typed language needs something like that:

foo = "hello"
bar = 1337
foo + str(bar)

Note that this is a spectrum, but the example from above is where I draw the line.

I'd say strong is to be prefered, but not too strong. For example, if I add a float and a double I would not want to get an exception. Maybe.

Explicit vs Implicit Typing

In an explicitly typed language, all variable types are directly given by the developer.

In an implicitly typed language, the language can infer some of the types.

Note that dynamically typed languages are always implicitly typed. Python likes to call that "duck typing": If it walks like a duck and quacks like a duck, it is a duck.

Overview

Language Static Strength Type Expression
Rust static strong implicit
C++ static strong explicit
C static weak explicit
Java static strong explicit
Python dynamic strong implicit, since Python 3.5 optionally explicit
JavaScript dynamic weak implicit
PHP dynamic weak implicit

See also

  • Wikipedia: Comparison of programming languages by type system

Published

Nov 26, 2018
by Martin Thoma

Category

Code

Tags

  • Programming Languages 1

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor