liveBook · Manning

Type system

As explained in Functional Programming in Scala, Second Edition

A type system is the set of language rules and mechanisms that classify program expressions and constrain their valid composition.

A type system classifies expressions and restricts how they may compose. In Scala, a type signature can make an implementation almost inevitable: a function returning B => C must return a function accepting B and producing C, often by applying the available function f. section 2.5

Following types to an implementation

Given def partial1[A, B, C](a: A, f: (A, B) => C): B => C, first return a function taking b: B. Its result must be C, and f is the available operation that produces C from a and b, yielding (b: B) => f(a, b). The same reasoning supports curry, uncurry, and compose. section 2.5

Using types to protect local state

The ST design uses types to prevent local mutable references from escaping. ST[S, STRef[S, Int]] is unsafe to run because its result exposes S, while ST[S, Int] is safe because it returns only an Int. A polymorphic action [s] => () => ST[s, A] can therefore be run without revealing its private state. section 14.2.3

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Works through function signatures and compiler constraints, then applies polymorphic types to prevent STRef escape.
Examples
Uses partial1, curry, uncurry, compose, STRef actions, safe ST.run calls, and rejected escaping-reference programs.
What it assumes
Assumes basic Scala functions and types while developing higher-order-function reasoning and polymorphic state-safety techniques.
What you can do after
Build implementations from signatures and diagnose unsafe state programs through type mismatches.
How it connects
Connects higher-order functions, partial application, polymorphism, referential transparency, mutable state, and the ST monad.

Also explained in

Compare how 6 books explain type system →

Related concepts

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