liveBook · Manning

Dynamic typing

As explained in Elixir in Action, Third Edition

A dynamic language is a programming language in which important type checks or type determinations occur at runtime rather than being fully enforced statically.

Dynamic typing determines a variable’s type from the value it holds at runtime, without explicit variable or type declarations. In Elixir, assignment is binding: names can be rebound, while the immutable data they reference is not modified. section 2.2 section 2.8

Because function inputs and outputs are not obvious from signatures alone, typespecs can document expected types and support Dialyzer-based static analysis. They compensate for, but do not replace, a static type system. section 2.3.6

Runtime values and optional type information

A name can first bind to 10,000 and later to 11,000. Separately, a function can declare @spec area(number) :: number, giving tools and readers an explicit contract while the language remains dynamically typed. section 2.2 section 2.3.6

defmodule Circle do
  @pi 3.14159
 
  @spec area(number) :: number            #1
  def area(r), do: r*r*@pi
 
  @spec circumference(number) :: number   #2
  def circumference(r), do: 2*r*@pi
end

Shows a typespec attached to functions that accept and return numbers. section 2.3.6

How Elixir in Action, Third Edition covers it

How deep it goes
Defines runtime type determination, then reaches documentation and static analysis through typespecs without developing a static type system.
Examples
Uses rebinding of a salary value and numeric Circle functions with optional typespec declarations.
What it assumes
Assumes basic expressions and functions while teaching variable binding, rebinding, and runtime type determination.
What you can do after
Evaluate runtime typing and document function contracts with typespecs and Dialyzer.
How it connects
Connects dynamic typing with variable binding, immutable data, typespecs, and static analysis.

Also explained in

Compare how 6 books explain dynamic typing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.