LearnViz CompSci
Home / Learn / Languages & Compilers / Linear and Affine Types

Advanced Topic 46 of 50 Languages & Compilers

Linear and Affine Types

Types restricting how many times a value is used.

How it works

Linear types require that every value be used exactly once, and affine types allow at most once, so a value can be dropped but not duplicated. This tracks resources through a program: after a value is consumed or moved, it cannot be used again, making it possible to enforce protocols and manage memory, file handles and locks statically. Rust's ownership is based on affine types, and Haskell's LinearTypes extension, Idris 2 and Clean explore linearity.

Step-by-step walkthrough

  1. A linear value must be used exactly once.
  2. Using it consumes it.
  3. A second use is a compile-time error.
  4. Affine types allow at most one use, which is Rust's moves.
  5. They model files, locks and memory safely.

Where it is used

Preventing use-after-close of files, safe in-place updates of data structures, session types for communication protocols and memory safety without garbage collection use linear and affine types.

Common pitfall

Code must thread resources through functions explicitly, which can be awkward, and borrowing mechanisms are needed to make them practical. Copyable values need to be distinguished from non-copyable ones. Error messages about moved values confuse newcomers. Understand the difference between must-use and may-drop semantics.

Where it fits in the course

Linear and Affine Types is topic 46 of 50 in Languages & Compilers, rated Advanced. It follows Dependent Types. The next topic is Garbage Collection Algorithms.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Languages & Compilers quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Linear and Affine Types?
Types restricting how many times a value is used. It belongs to Languages & Compilers and is an advanced-level topic.
Where do people use Linear and Affine Types?
Preventing use-after-close of files, safe in-place updates of data structures, session types for communication protocols and memory safety without garbage collection use linear and affine types.
What is the most common mistake with Linear and Affine Types?
Code must thread resources through functions explicitly, which can be awkward, and borrowing mechanisms are needed to make them practical.
What should I learn before this topic?
Start with Dependent Types, the topic just before it in Languages & Compilers.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Languages & Compilers quiz.
Is Linear and Affine Types hard to learn?
It is an Advanced topic. Expect to read the explanation more than once and to connect it with earlier topics. Working through a small example by hand helps most.
How long does it take to learn Linear and Affine Types?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Linear and Affine Types?
Dependent Types, Garbage Collection Algorithms, Hindley-Milner Inference are closely connected. The full list is in the related topics section of this page.
What should I study after Linear and Affine Types?
Continue with Garbage Collection Algorithms, the next topic in Languages & Compilers.

Related topics