liveBook · Manning

Type safety

As explained in Fabulous Adventures in Data Structures and Algorithms

Type safety is the property that a program's operations respect the constraints imposed by its types.

Type safety means that program operations respect the constraints imposed by their types. Generic data structures can make those constraints enforceable at compile time, rather than leaving incompatible assignments to fail later. section 1.0 section 3.10

Variance exposes the boundary clearly. A mutable IList<T> cannot safely be treated as an IList of a broader type: adding a Giraffe through that view can place an invalid value into a list of Tigers. C# therefore restricts covariant type parameters to output positions. section 2.3

Immutability removes the mutation that causes the violation, but Push(T) still consumes T. Moving Push into an extension method leaves the interface read-only and permits safe covariance through IEnumerable<T>, Peek(), and Pop(). section 2.3

A covariant immutable stack

An IImStack<Tiger> can be assigned to IImStack<Animal>, after which an immutable Push extension can add a Giraffe. The original stack remains structurally safe because the interface exposes no mutating Add operation. section 2.3

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Demonstrates unsafe and safe covariance with C# interfaces, mutation, output positions, immutability, and extension methods.
Examples
Uses an IList<Tiger> covariance counterexample and a covariant immutable stack accepting Tigers and Giraffes.
What it assumes
Assumes familiarity with C# generics, interfaces, inheritance, and immutable linked-list structures.
What you can do after
Apply via C# generic interfaces and extension methods that preserve safe assignments across immutable data structures.
How it connects
Connects type safety with generic types, covariance, immutability, recursive types, and data structures.

Also explained in

Compare how 8 books explain type safety →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.