Programming Search⌘K

Conceptcore

Type inference

Also called Hindley-Milner type inference, Damas-Milner, algorithm W, let-polymorphism, local type inference.

Type inference lets the compiler find static types without annotations. Local inference reads one initializer. Hindley-Milner inference solves type equations with unification.

Applies to
core-languages

Idea

A statically typed program gives each variable and each expression a type, and the compiler checks the types before the program runs. Type inference lets the compiler find many of these types itself. The programmer writes fewer annotations, and the check stays the same.

There are two main kinds. Local inference finds the type of one declaration from its initializer. Java var, C# var, C++ auto and Go := work this way.15 Global inference finds the types of a whole group of definitions at once, functions included, from the way the code uses each value. ML and Haskell use the Hindley-Milner method, which solves equations between types with unification.28

In the Java program below, no local variable has a written type. The compiler gives counts the type HashMap<String, Integer>, words the type String[] and total the type int.17

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        var counts = new HashMap<String, Integer>();
        var words = "to be or not to be".split(" ");
        for (var word : words) {
            counts.merge(word, 1, Integer::sum);
        }
        var total = 0;
        for (var n : counts.values()) {
            total += n;
        }
        System.out.println(counts.get("to") + " " + total + " " + counts.getClass().getSimpleName());
    }
}
Output
2 6 HashMap

The type is fixed when the program is compiled. A later assignment of a value of another type does not compile, as it would with a written type. The C# reference says it directly: implicitly typed local variables are strongly typed.18 Type inference is a feature of static type systems. Static and dynamic typing explains the difference from a dynamically typed language, where a variable has no static type at all.

How it works

Local inference

Local inference looks at one declaration. The type of the variable is the type of its initializer, after a few adjustments that each language defines.

  • Java. The type of a var variable is the type of the initializer, after an upward projection that removes capture variables. A var declaration with no initializer, with the null literal, or with a lambda expression is a compile-time error.1617 JEP 286 gives the reason for the required initializer: it avoids inference errors caused by code far from the declaration.15
  • C#. var x;, var z = null; and var u = x => x + 1; are errors. The reason for null is that null does not have a type.19
  • C++. The type of auto comes from the rules for template argument deduction in a function call. The compiler treats auto x = e; like a call f(e) of an invented template<class U> void f(U u).20 When the parameter is not a reference, the deduction ignores the top-level const and turns an array into a pointer.21 The type of an expression is never a reference type, so plain auto makes a copy.39 auto& keeps the reference.
  • Go. x := e is a short form of var x = e.22 An untyped constant gets its default type: bool, rune, int, float64, complex128 or string.23
  • TypeScript. A variable gets the type of its initializer, and a function gets the return type of its return statements.27 A let that holds "Hello World" gets the type string. A const gets the literal type "Hello World", because its value cannot change.26 For an array of mixed elements, the compiler picks a common type of the elements, so [0, 1, null] has the type (number | null)[].24 Inference also works from the outside in. In window.onmousedown = function (mouseEvent) {...}, the location of the function gives mouseEvent its type. The handbook calls this contextual typing.25
  • Kotlin. val and var take the type of their initializer. A function with an expression body gets its return type by inference, but a function with a block body does not.3233
  • C. C23 adds the storage-class specifier auto for inference. The type is the type of the initializer after lvalue, array to pointer and function to pointer conversion.36

Global inference with unification

Hindley-Milner inference does not stop at one declaration. It gives every variable and every parameter a type, also when no initializer exists. The method has four steps, and the worked example below numbers them.

  • Type variables. Each unknown type gets a fresh type variable, such as t1.
  • Constraints. Each use of a value adds an equation. A call f x says that the type of f is type of x -> t2 for a fresh t2, the type of the result.
  • Unification. Robinson's unification algorithm solves the equations. Its result is a substitution: a map from type variables to types. Unification finds the most general substitution, so the result has no choice in it that the program did not force.47
  • Generalization. At a let, each type variable that is not in the types of the enclosing variables becomes generic. Each later use of the name gets a fresh copy of the generic variables.310

Hindley used unification in this way for combinatory logic in 1969. Milner found the method again for ML and called his algorithm W.12 The worked example below infers the type of twice, a function that applies f two times.

expression   fn f => fn x => f (f x)

step 1   f : t1          x : t2
step 2   f x             t1 = t2 -> t3
         f (f x)         t1 = t3 -> t4
step 3   unify  t2 -> t3  with  t3 -> t4
           t2 = t3,  t3 = t4
         substitution    t3 := t2,  t4 := t2,  t1 := t2 -> t2
         whole type      t1 -> t2 -> t4  becomes  (t2 -> t2) -> t2 -> t2
step 4   no type variable occurs in the environment, so all are generic
         twice : for all a. (a -> a) -> a -> a

The occurs check. Unification must not bind a type variable to a type that contains the same variable. In fn x => x x, the parameter x has the type t1. The call x x asks for t1 = t1 -> t2. No finite type solves this equation, so inference fails.

Let-polymorphism and lambda-bound monomorphism. Only a name bound by let becomes generic. A parameter of a function stays monomorphic: each use inside the body has the same type. In Milner's map example, the two uses of map get different instances, but the parameter f must have the same type at each use.3 The Haskell report shows the same rule. Inside f x = let g y z = ([x,y], z) in ..., the type of g is generic in b but not in a, because a is the type of x in the environment. So (g True, g 'c') is invalid.11

The Python program below is a small version of algorithm W. Expressions are tuples. The program keeps one substitution for the whole run and extends it at each step of unification. It infers four types, accepts id at two types under let, rejects the same code with id as a parameter, and stops at the occurs check.

import itertools


class TVar:
    def __init__(self, id):
        self.id = id


class TCon:
    def __init__(self, name, *args):
        self.name = name
        self.args = args


INT = TCon("int")
BOOL = TCon("bool")


def arrow(a, b):
    return TCon("->", a, b)


class Scheme:
    def __init__(self, generic, type):
        self.generic = generic  # ids of the generic type variables
        self.type = type


class InferError(Exception):
    pass


def show(t, names):
    if isinstance(t, TVar):
        return names.setdefault(t.id, chr(ord("a") + len(names)))
    if not t.args:
        return t.name
    left, right = (show(a, names) for a in t.args)
    if t.args[0].__class__ is TCon and t.args[0].args:
        left = f"({left})"
    return f"{left} {t.name} {right}"


class Inferer:
    def __init__(self):
        self.subst = {}  # type variable id -> type
        self.ids = itertools.count()

    def fresh(self):
        return TVar(next(self.ids))

    def resolve(self, t):
        if isinstance(t, TVar):
            if t.id in self.subst:
                return self.resolve(self.subst[t.id])
            return t
        return TCon(t.name, *(self.resolve(a) for a in t.args))

    def occurs(self, id, t):
        if isinstance(t, TVar):
            return t.id == id
        return any(self.occurs(id, a) for a in t.args)

    def unify(self, a, b):
        a, b = self.resolve(a), self.resolve(b)
        names = {}
        if isinstance(a, TVar):
            if isinstance(b, TVar) and a.id == b.id:
                return
            if self.occurs(a.id, b):
                raise InferError(f"occurs check: {show(a, names)} occurs in {show(b, names)}")
            self.subst[a.id] = b
        elif isinstance(b, TVar):
            self.unify(b, a)
        elif a.name != b.name or len(a.args) != len(b.args):
            raise InferError(f"cannot unify {show(a, names)} with {show(b, names)}")
        else:
            for x, y in zip(a.args, b.args):
                self.unify(x, y)

    def free(self, t):
        t = self.resolve(t)
        if isinstance(t, TVar):
            return {t.id}
        return set().union(*(self.free(a) for a in t.args))

    def generalize(self, env, t):
        in_env = set()
        for s in env.values():
            in_env |= self.free(s.type) - s.generic
        return Scheme(self.free(t) - in_env, self.resolve(t))

    def instantiate(self, s):
        fresh = {id: self.fresh() for id in s.generic}

        def copy(t):
            t = self.resolve(t)
            if isinstance(t, TVar):
                return fresh.get(t.id, t)
            return TCon(t.name, *(copy(a) for a in t.args))

        return copy(s.type)

    def infer(self, env, e):
        kind = e[0]
        if kind == "int":
            return INT
        if kind == "bool":
            return BOOL
        if kind == "var":
            return self.instantiate(env[e[1]])
        if kind == "fn":
            _, x, body = e
            tx = self.fresh()  # a parameter is never generic
            return arrow(tx, self.infer({**env, x: Scheme(set(), tx)}, body))
        if kind == "app":
            _, f, a = e
            tf = self.infer(env, f)
            ta = self.infer(env, a)
            result = self.fresh()
            self.unify(tf, arrow(ta, result))
            return result
        _, x, bound, body = e  # let
        t = self.infer(env, bound)
        return self.infer({**env, x: self.generalize(env, t)}, body)


def src(e):
    kind = e[0]
    if kind == "int":
        return str(e[1])
    if kind == "bool":
        return "true" if e[1] else "false"
    if kind == "var":
        return e[1]
    if kind == "fn":
        return f"fn {e[1]} => {src(e[2])}"
    if kind == "app":
        f, a = src(e[1]), src(e[2])
        if e[1][0] in ("fn", "let"):
            f = f"({f})"
        if e[2][0] in ("fn", "let", "app"):
            a = f"({a})"
        return f"{f} {a}"
    return f"let {e[1]} = {src(e[2])} in {src(e[3])}"


def var(name):
    return ("var", name)


def app(f, *args):
    for a in args:
        f = ("app", f, a)
    return f


ENV = {
    "pair": Scheme({-1, -2}, arrow(TVar(-1), arrow(TVar(-2), TCon("*", TVar(-1), TVar(-2))))),
    "add": Scheme(set(), arrow(INT, arrow(INT, INT))),
}

identity = ("fn", "x", var("x"))
use_twice = app(var("pair"), app(var("id"), ("int", 1)), app(var("id"), ("bool", True)))
examples = [
    identity,
    ("fn", "x", app(var("add"), var("x"), ("int", 1))),
    ("fn", "f", ("fn", "x", app(var("f"), app(var("f"), var("x"))))),
    ("fn", "f", ("fn", "g", ("fn", "x", app(var("g"), app(var("f"), var("x")))))),
    ("let", "id", identity, use_twice),
    ("fn", "id", use_twice),
    ("fn", "x", app(var("x"), var("x"))),
]

for e in examples:
    inferer = Inferer()
    try:
        t = inferer.infer(ENV, e)
        print(f"{src(e)}\n  : {show(inferer.resolve(t), {})}")
    except InferError as err:
        print(f"{src(e)}\n  error: {err}")
Output
fn x => x
  : a -> a
fn x => add x 1
  : int -> int
fn f => fn x => f (f x)
  : (a -> a) -> a -> a
fn f => fn g => fn x => g (f x)
  : (a -> b) -> (b -> c) -> a -> c
let id = fn x => x in pair (id 1) (id true)
  : int * bool
fn id => pair (id 1) (id true)
  error: cannot unify int with bool
fn x => x x
  error: occurs check: a occurs in a -> b

The third result is the type of the worked example. Under let, id has the scheme for all a. a -> a, so the two uses get the types int -> int and bool -> bool. As a parameter, id has one type variable, and the second use forces int to equal bool.

Inference inside a function body

Rust takes a middle way. The compiler infers the types of the local variables in a function body, also from later uses. But each function signature must give the types of its parameters and its result. The Rust Book calls this a deliberate decision: the annotations in signatures mean that the compiler almost never needs them elsewhere.2930 An empty vector shows inference from a later use. let mut v = Vec::new(); followed by v.push(5); needs no annotation, because the push decides the element type.31

In languages

LanguageVerdictNote
PythonLibrary
C++Native
JavaNative
CNative
C#Native
JavaScriptDoes not applyJavaScript has no static types, so there is no static type to infer; a variable can hold a value of any type.
TypeScriptNative
GoNative
RustNative
KotlinNativeVal and var take the type of their initializer, and a function with an expression body gets an inferred return type; no example file because kotlinc is not installed on the machine that recorded the examples.
SQLDoes not applyA query declares no variables; the types of the columns come from the table definitions.
BashDoes not applyBash has no types, only strings and arrays of strings.
PowerShellDoes not applyPowerShell variables are loosely typed and hold a value of any .NET type; a type attribute such as [int] constrains a variable and converts each value that is assigned to it.
HTMLDoes not applyMarkup has no types.
CSSDoes not applyStyle rules have no types.
Regular expressionsDoes not applyA pattern has no types.

The example files show inference in each language. The table gives the verdict for each tier 1 language.

Language Verdict Form
Python library CPython infers nothing; a type checker such as mypy infers a type from the first value34
JavaScript n/a no static types
TypeScript native initializers, return statements, contextual typing, literal widening
Java native var for local variables with an initializer16
C native C23 auto in an object definition with an initializer36
C++ native auto and decltype(auto), with the rules of template argument deduction20
C# native var for local variables with an initializer19
Go native := and var with no type; untyped constants get default types2223
Rust native inference inside a function body; signatures need types30
Kotlin native val and var with an initializer; the return type of an expression body3233
PowerShell n/a variables are loosely typed and hold a value of any .NET type37
Bash n/a no static types
SQL n/a a query declares no variables; column types come from the table definitions
Regex n/a a pattern has no types
HTML/CSS n/a markup and style rules have no types

The Python runtime does not enforce type annotations.40 A separate type checker reads them. mypy infers the type of a variable from the value that is first assigned to it. It cannot always infer the type of an empty list or dictionary, and it then asks for an annotation, as in Need type annotation for "l".3435

PythonGitHub
count = 1
print(type(count).__name__)
count = "one"
print(type(count).__name__)

items = []
items.append(3)
print(items, type(items[0]).__name__)


def double(n):
    return n * 2


print(double(4), double("ab"))
Output
int
str
[3] int
8 abab
  • Verdict: library. CPython does not infer or check static types. The program runs, and each name holds a value of any type.
  • A type checker such as mypy infers the type int for count from its first value.
  • mypy cannot infer the element type of items = [] and asks for an annotation, such as items: list[int] = [].
  • A function with no annotations, such as double, accepts any argument at run time.
#include <print>
#include <type_traits>
#include <vector>

int main() {
    const int limit = 10;
    int values[3] = {1, 2, 3};
    std::vector<int> v{4, 5};
    int& first = v[0];

    auto a = limit;
    auto b = values;
    auto c = first;
    auto& d = first;
    decltype(auto) e = (v[1]);
    const auto& f = limit;

    std::println("{}", std::is_same_v<decltype(a), int>);
    std::println("{}", std::is_same_v<decltype(b), int*>);
    std::println("{}", std::is_same_v<decltype(c), int>);
    std::println("{}", std::is_same_v<decltype(d), int&>);
    std::println("{}", std::is_same_v<decltype(e), int&>);
    std::println("{}", std::is_same_v<decltype(f), const int&>);
}
Output
true
true
true
true
true
true
  • Verdict: native. auto follows the rules of template argument deduction for a parameter passed by value.
  • Plain auto drops the top-level const and the reference, and turns an array into a pointer.
  • auto& and const auto& keep a reference. decltype(auto) keeps the type that decltype gives, here int& for the expression (v[1]).
JavaGitHub
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        var names = new ArrayList<String>();
        names.add("Ada");
        names.add("Grace");
        var lengths = Map.of("Ada", 3);
        var total = 0L;
        for (var name : names) {
            total += name.length();
        }
        var first = names.get(0);
        System.out.println(names.getClass().getSimpleName() + " " + total + " " + first.toUpperCase() + " " + lengths.get("Ada"));
        System.out.println(((Object) total).getClass().getSimpleName());
    }
}
Output
ArrayList 8 ADA 3
Long
  • Verdict: native. var takes the type of its initializer: ArrayList<String>, Map<String, Integer>, long and String.
  • var works for local variables and in for loops. Fields, parameters and return types still need written types.
  • var needs an initializer, and the initializer must not be null or a lambda expression (see Pitfalls).
  • var is a reserved type name, not a keyword, so older code with a variable named var still compiles.
#include <stdio.h>

int main(void) {
    auto x = 3.5;
    auto n = 7 / 2;
    double a[3] = {1, 2, 3};
    auto p = a;
    auto q = &a;
    printf("%g %d\n", x, n);
    printf("%zu %zu %zu\n", sizeof x, sizeof p, sizeof *q);
    printf("%d\n", _Generic(n, int: 1, default: 0));
}
Output
3.5 3
8 8 24
1
  • Verdict: native. C23 infers the type of an object from its initializer when the declaration has the storage-class specifier auto.
  • The type comes after array to pointer conversion. p is a double *, but q is a pointer to the whole array of three double values.
  • 7 / 2 has the type int, so n is an int and holds 3.
  • The program needs -std=c23.
var count = 32;
var ratio = 2.5;
var names = new List<string> { "Ada", "Grace" };
var pairs = names.Select(n => (n, n.Length)).ToList();
var make = () => 1;

Console.WriteLine(count.GetType());
Console.WriteLine(ratio.GetType());
Console.WriteLine(names.GetType());
Console.WriteLine(pairs[1]);
Console.WriteLine(pairs[1].GetType());
Console.WriteLine(make.GetType());
Output
System.Int32
System.Double
System.Collections.Generic.List`1[System.String]
(Grace, 5)
System.ValueTuple`2[System.String,System.Int32]
System.Func`1[System.Int32]
  • Verdict: native. var takes the type of the initializer, and the variable is strongly typed.
  • The lambda () => 1 has a natural type, Func<int>. A lambda with an untyped parameter, such as x => x + 1, has none, and var rejects it.
  • var x; and var z = null; do not compile, because the compiler has no type to use.
  • The tuple (n, n.Length) gets the type (string, int) from its parts, which is ValueTuple<string, int> at run time.
TypeScriptGitHub
let mode = "read";
const fixed = "read";
mode = "write";

const values = [0, 1, null];
const names = ["Ada", "Grace"];
const lengths = names.map((name) => name.length);

function half(n: number) {
  return n / 2;
}

console.log(mode, fixed, values.length, lengths.join(","), half(5));
console.log(typeof half(5), typeof fixed);
Output
write read 3 3,5 2.5
number string
  • Verdict: native. mode has the type string, fixed has the literal type "read", and values has the type (number | null)[].
  • The parameter name of the arrow function gets the type string from its context, the call of map on a string[].
  • half gets the return type number from its return statement.
  • The runner removes the types and does not check them. The output shows only the values, which are the same as in JavaScript.
package main

import "fmt"

func main() {
	n := 1 << 3
	f := 2.5
	r := 'a'
	c := 1i
	s := "go"
	ok := n > 4
	var m = n * 2
	fmt.Printf("%T %T %T %T %T %T %T\n", n, f, r, c, s, ok, m)

	const big = 1 << 40
	var small int8 = big >> 34
	fmt.Println(small)
}
Output
int float64 int32 complex128 string bool int
64
  • Verdict: native. := and var with no type take the type of the initializer.
  • An untyped constant gets its default type. 1 << 3 is an int, 2.5 a float64, 'a' a rune (int32), and 1i a complex128.
  • An untyped constant has no fixed type until a use needs one. big >> 34 fits in int8, so the assignment compiles.
  • := works only inside functions. In an inner block it declares another variable (see Pitfalls).
RustGitHub
use std::any::type_name;

fn type_of<T>(_: &T) -> &'static str {
    type_name::<T>()
}

fn mean(values: &[f64]) -> f64 {
    let total: f64 = values.iter().sum();
    total / values.len() as f64
}

fn main() {
    let mut v = Vec::new();
    v.push(5u8);
    let n = 7;
    let x = 2.5;
    let words: Vec<_> = "a bb ccc".split(' ').collect();
    println!("{}", type_of(&v));
    println!("{} {}", type_of(&n), type_of(&x));
    println!("{}", type_of(&words));
    println!("{}", mean(&[1.0, 2.0, 6.0]));
}
Output
alloc::vec::Vec<u8>
i32 f64
alloc::vec::Vec<&str>
3
  • Verdict: native. Inference works inside a function body. The later push of a u8 fixes the type of v.
  • An integer literal with no other constraint is an i32, and a floating-point literal is an f64.
  • Vec<_> asks the compiler to infer only the element type. collect needs to know the collection type.
  • Each signature, such as mean, gives the types of its parameters and its result. The compiler does not infer them.
  • Local and global inference. Local inference needs an initializer and looks only at one declaration.15 Hindley-Milner inference also finds the types of function parameters from the uses in the body.3 Rust infers inside a body and requires types at the boundary of each function.30
  • Principal types. Take an expression that has a type. Hindley-Milner inference finds one type for it, and each other type of the expression is an instance of that type. Damas and Milner proved that algorithm W finds it.6 The Haskell report states that its type system can infer the principal type of all expressions.9
  • Contextual typing. TypeScript also sends type information from the outside in, from the expected type to a function expression.25
  • The value restriction. In ML, a reference cell must not become polymorphic. Otherwise code could store a value of one type and read it as another. OCaml does not generalize the type of a mutable value or of the result of a function call. The type of ref None gets a weak type variable, written '_weak1, that the first use fixes.1413 The call (fun x -> x) (fun x -> x) also has a weak type. Eta expansion is the fix.13
  • The monomorphism restriction. Take a binding of one variable with no arguments, such as n = e. Haskell does not generalize a constrained type variable of such a binding unless the binding has a type signature.12
  • Type classes. Haskell extends Hindley-Milner inference with class constraints, such as Eq a, in the inferred type.8 Generics explains generic types and constraints.
  • Subtyping. Hindley-Milner inference solves equations between types. With Subtyping, one value can have many types. For an array literal, TypeScript picks a common type of the elements, and that type can be a union.24

Pitfalls

var needs an initializer with a type

Java infers the type of var only from the initializer. The null literal has the null type, and the JLS makes it an error for var.17 C# rejects var z = null; for the same reason.19

This code does not compile, on purpose.

public class Main {
    public static void main(String[] args) {
        var name = null;
        name = "Ada";
        System.out.println(name);
    }
}

javac stops with cannot infer type for local variable name and the reason variable initializer is 'null'. The fix is a written type, String name = null;, or a real initial value.

Rust cannot choose among many types

parse can return many types. When nothing in the function body fixes the type, the compiler stops and asks for an annotation.29

This code does not compile, on purpose.

fn main() {
    let guess = "42".parse().expect("Not a number!");
    println!("{}", guess);
}

rustc stops with error E0284, type annotations needed. The fix is an annotation on the variable, let guess: u32, or a turbofish on the call, "42".parse::<u32>().

fn main() {
    let guess: u32 = "42".parse().expect("Not a number!");
    let other = "7".parse::<u8>().expect("Not a number!");
    println!("{} {}", guess + 1, other);
}
Output
43 7

C++ auto drops the reference

auto deduces a type as a template parameter U by value. The reference and the top-level const of the initializer are gone, so the variable is a copy.2021 Here account is a copy of the element, and the update does not reach the vector.

This code runs but gives a wrong answer on purpose.

#include <print>
#include <string>
#include <vector>

struct Account {
    std::string owner;
    int balance;
};

Account& find(std::vector<Account>& accounts, const std::string& owner) {
    for (auto& a : accounts) {
        if (a.owner == owner) {
            return a;
        }
    }
    return accounts.front();
}

int main() {
    std::vector<Account> accounts{{"ada", 100}, {"grace", 50}};
    auto account = find(accounts, "grace");
    account.balance += 25;
    std::println("{}", accounts[1].balance);
}
Output
50

The program prints the old balance. The fix is auto& account = find(accounts, "grace");. decltype(auto) also keeps the reference, because it deduces the type that decltype gives for the initializer.20

#include <print>
#include <string>
#include <vector>

struct Account {
    std::string owner;
    int balance;
};

Account& find(std::vector<Account>& accounts, const std::string& owner) {
    for (auto& a : accounts) {
        if (a.owner == owner) {
            return a;
        }
    }
    return accounts.front();
}

int main() {
    std::vector<Account> accounts{{"ada", 100}, {"grace", 50}};
    auto& account = find(accounts, "grace");
    account.balance += 25;
    std::println("{}", accounts[1].balance);
}
Output
75

Go := in an inner block declares a second variable

:= may redeclare a variable only when the variable was declared earlier in the same block.22 In an inner block, := declares another variable, and that variable hides the outer one until the block ends.38 Here limit, err := inside the if makes a second limit.

This code runs but gives a wrong answer on purpose.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	limit := 10
	input := "25"
	if input != "" {
		limit, err := strconv.Atoi(input)
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println("inside:", limit)
	}
	fmt.Println("limit:", limit)
}
Output
inside: 25
limit: 10

The outer limit keeps the value 10. The fix is to declare err before the block and to assign with =.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	limit := 10
	input := "25"
	if input != "" {
		var err error
		limit, err = strconv.Atoi(input)
		if err != nil {
			fmt.Println(err)
			return
		}
	}
	fmt.Println("limit:", limit)
}
Output
limit: 25

TypeScript falls back to any

When TypeScript has no annotation and no context for a type, it usually uses any, and any is not type-checked.28 A parameter with no annotation is such a case. Here both parameters of addPoints are any, so the compiler accepts a string, and + joins two strings.

This code runs but gives a wrong answer on purpose.

function addPoints(score, bonus) {
  return score + bonus;
}

const fields = "40,2".split(",");
console.log(addPoints(fields[0], 2));
Output
402

The fix is a type on each parameter, a conversion at the call, and the compiler option noImplicitAny, which makes each implicit any an error.28

function addPoints(score: number, bonus: number): number {
  return score + bonus;
}

const fields = "40,2".split(",");
console.log(addPoints(Number(fields[0]), 2));
Output
42

Cost

  • Run time. Inference happens when the program is compiled. An inferred variable has a static type, as a variable with a written type has.1718
  • Error messages. A type error found by unification can show far from its cause. A constraint from one line can fix a type that another line uses. JEP 286 requires an initializer for var to avoid such errors.15 The Rust Book adds that the compiler gives more helpful error messages when it knows the types that a function expects.30
  • Readability. The type of an inferred variable is not on the page. The TypeScript handbook notes that some code bases write return types for documentation and to prevent accidental changes.27
  • Expressiveness. Hindley-Milner inference needs no annotations, but it gives a parameter only one type in the body.3 A mutable cell or a function result is not generalized under the value restriction.13

History

  • 1965. J. A. Robinson published the unification algorithm as a part of resolution in logic.74
  • 1969. J. Roger Hindley showed that an object of combinatory logic has a principal type scheme, found with unification.12
  • 1978. Robin Milner published algorithm W and the type discipline of ML. ML had used the discipline for nearly two years. The paper proves that well-typed programs cannot go wrong.25
  • 1982. Luis Damas and Robin Milner proved that algorithm W finds the principal type scheme.6
  • 2010. The Haskell 2010 report describes its type system as a traditional Hindley-Milner polymorphic type system.8
  • 2023. The C23 working draft N3096, of April 2023, includes type inference with auto.36

References

  1. J. Roger Hindley, The Principal Type-Scheme of an Object in Combinatory Logic, Transactions of the American Mathematical Society 146, 1969, the principal type-scheme of an object, found with the unification algorithm (as summarized in Milner 1978, section 1)
  2. Robin Milner, A theory of type polymorphism in programming, Journal of Computer and System Sciences 17(3), 1978, 1 Introduction (ML had used the discipline for nearly 2 years; Hindley was the first to notice that Robinson's unification algorithm fits the problem)
  3. Robin Milner, A theory of type polymorphism in programming, Journal of Computer and System Sciences 17(3), 1978, 2 Illustrations of the type discipline, Example 1 (map) and Example 2 (tagpair, generic type variables)
  4. Robin Milner, A theory of type polymorphism in programming, Journal of Computer and System Sciences 17(3), 1978, 4.1 The Algorithm W (Proposition 5, the unification theorem of Robinson; W is probably complete)
  5. Robin Milner, A theory of type polymorphism in programming, Journal of Computer and System Sciences 17(3), 1978, the abstract (Semantic Soundness Theorem, well-typed programs cannot go wrong; W implemented for ML in Edinburgh LCF)
  6. Luis Damas and Robin Milner, Principal type-schemes for functional programs, POPL 1982, pages 207 to 212, the sections Algorithm W and Completeness of W
  7. J. A. Robinson, A Machine-Oriented Logic Based on the Resolution Principle, Journal of the ACM 12(1), 1965, the unification algorithm, which finds a most general unifier (as stated in Milner 1978, Proposition 5)
  8. Haskell 2010 Language Report, chapter 4, Declarations and Bindings, 4.1 Overview of Types and Classes (a traditional Hindley-Milner polymorphic type system)
  9. Haskell 2010 Language Report, chapter 4, Declarations and Bindings, 4.1.4 Semantics of Types and Classes (the principal type of all expressions)
  10. Haskell 2010 Language Report, chapter 4, Declarations and Bindings, 4.5.2 Generalization
  11. Haskell 2010 Language Report, chapter 4, Declarations and Bindings, 4.5.4 Monomorphism (g is monomorphic in a; (g True, g 'c') is invalid)
  12. Haskell 2010 Language Report, chapter 4, Declarations and Bindings, 4.5.5 The Monomorphism Restriction, Rule 1
  13. The OCaml Manual, release 5.3, Polymorphism and its limitations, 6.1.2 The value restriction
  14. The OCaml Manual, release 5.3, Polymorphism and its limitations, 6.1.1 Weak polymorphism and mutation (let store = ref None)
  15. JEP 286: Local-Variable Type Inference, Summary, Motivation (C++ auto, C# var, Go :=), Description and Scope Choices (an initializer avoids action at a distance errors); Release 10
  16. The Java Language Specification, Java SE 25 Edition, 14.4 Local Variable Declarations (the compile-time errors for var)
  17. The Java Language Specification, Java SE 25 Edition, 14.4.1 Local Variable Declarators and Types (the type of the initializer; the null type is an error)
  18. C# language reference, Declaration statements, Implicitly typed local variables (implicitly typed local variables are strongly typed)
  19. C# language specification, Statements, 13.6.2.2 Implicitly typed local variable declarations (var z = null is an error)
  20. Working Draft, Programming Languages, C++ (N4950, the C++23 draft), [dcl.type.auto.deduct], paragraph 3 (auto is deduced with the rules for template argument deduction from a function call)
  21. Working Draft, Programming Languages, C++ (N4950, the C++23 draft), [temp.deduct.call], paragraph 2 (if P is not a reference type, arrays and functions decay and top-level cv-qualifiers are ignored)
  22. The Go Programming Language Specification, Short variable declarations
  23. The Go Programming Language Specification, Constants (the default types of untyped constants)
  24. TypeScript Handbook, Type Inference, Best common type
  25. TypeScript Handbook, Type Inference, Contextual Typing
  26. TypeScript Handbook, Everyday Types, Literal Types (let gives string, const gives the literal type)
  27. TypeScript Handbook, Everyday Types, Return Type Annotations
  28. TypeScript Handbook, Everyday Types, noImplicitAny
  29. The Rust Programming Language, 3.2 Data Types, Data Types (the compiler can usually infer the type; parse needs an annotation)
  30. The Rust Programming Language, 3.3 Functions, Parameters (a deliberate decision in Rust's design)
  31. The Rust Programming Language, 8.1 Storing Lists of Values with Vectors, Creating a New Vector
  32. Kotlin documentation, Basic syntax, Variables
  33. Kotlin documentation, Functions, Return types and Single-expression functions
  34. mypy documentation, Type inference and type annotations, Type inference
  35. mypy documentation, Type inference and type annotations, Explicit types for collections (Need type annotation for "l")
  36. ISO/IEC 9899:2023 working draft N3096 (C23), 6.7.9 Type inference
  37. PowerShell documentation, about_Variables, Types of variables
  38. The Go Programming Language Specification, Declarations and scope (an identifier may be redeclared in an inner block)
  39. Working Draft, Programming Languages, C++ (N4950, the C++23 draft), [expr.type], paragraph 1 (an expression of type reference to T is adjusted to T)
  40. The Python Standard Library, typing, Python 3.14, the note at the top of the page (the runtime does not enforce type annotations)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/EAVZR1/text.yaml