TL;DR
This paper introduces a novel approach to statically checking exceptions in Scala by treating them as "capabilities" that must be explicitly declared in type signatures. This system, implemented in Scala 3, aims to provide the safety of Java's checked exceptions without their notorious verbosity and inflexibility, while also proposing a more advanced type system to prevent these capabilities from escaping into closures.
What Problem Does This Paper Address?
Exceptions are a powerful tool for error handling, but their unchecked nature in many languages, including Scala, means that a function's contract regarding potential errors isn't statically enforced. This can lead to runtime surprises and makes code harder to reason about. Java's "checked exceptions" attempted to solve this by requiring exceptions to be declared, but their implementation proved cumbersome and inflexible, especially with higher-order functions. Monadic approaches like Try or Either offer an alternative but can lead to complex code and composition issues. This paper seeks a middle ground: a way to statically verify exception handling that is both safe and ergonomically sound.
Methods
The core of the proposed solution is the "effects as capabilities" pattern. Instead of declaring what exceptions a function throws, the system declares what capabilities it needs to throw them. This is achieved through:
CanThrow[E]Capability: A special type representing the ability to throw an exception of typeE. These are declared aserasedtypes, meaning they have no runtime overhead.throwsType: A syntactic sugar for context functions that implicitly require aCanThrowcapability. For example,def f(): Int throws Exceptionis equivalent todef f()(using CanThrow[Exception]): Int.tryExpression Augmentation:tryblocks now implicitly provideCanThrowcapabilities for the exceptions they catch, making them available within thetry's body.- Capture Tracking Type System (
CF<:□): To address the limitation where capabilities might escape through closures, a more advanced type system is proposed. This system tracks "capture sets" – the set of variables a value might capture. This allows for distinguishing between pure functions (that don't capture capabilities) and impure ones.
Datasets
Not discussed in the paper.
Key Insights and Ideas
- Capabilities over Effects: Framing exceptions as required capabilities rather than declared effects simplifies reasoning and integration with existing language features like implicit parameters.
- Erased Types for Zero Runtime Cost: Using
erasedtypes forCanThrowensures that the safety checks are purely compile-time, avoiding any performance penalty. - Gradual Migration: The
import scala.unsafeExceptions.canThrowAnymechanism provides an "escape hatch" for gradual adoption, allowing existing codebases to opt-in to safer exceptions incrementally. - Capture Sets for Purity: The advanced type system's ability to track captured variables is crucial for enforcing purity and preventing the escape of exception-throwing capabilities into closures, a key limitation of the initial capability-based approach.
- Boxing/Unboxing for Type System Compatibility: The introduction of boxed types and explicit unboxing operations is necessary to reconcile capturing types with existing type system constructs like type variables, which often require pure types.
Key Results
The paper presents a fully implemented scheme for Scala 3 that:
- Enables static checking of thrown exceptions through the
CanThrowcapability mechanism. - Provides a more ergonomic alternative to Java's checked exceptions.
- Introduces a capture-tracking type system (
CF<:□) that can prevent capabilities from escaping into closures, thus achieving a higher degree of safety. - Demonstrates the expressiveness and usability of the system through examples involving higher-order functions and safe exception handling.
Limitations and Future Work
- Ephemeral Capabilities: The initial capability-based approach has a loophole where ephemeral capabilities (those only valid within a
tryblock) might escape. The proposed capture-tracking type system aims to address this. - Manual Verification: For ephemeral capabilities, manual verification is currently required to ensure they don't escape.
- Mechanized Proofs: The authors acknowledge the need for formal proofs of the metatheory of the
CF<:□system. - Integration with Scala Features: Extending the capture tracking to other Scala constructs like assignment, classes, and traits is a significant area for future work.
- Type Inference: Developing and testing a robust type inferencer for capture sets is a key implementation goal.
- Library Migration: Migrating existing Scala libraries to leverage capture tracking will be a substantial undertaking.
- Broader Applicability: The authors suggest that capture tracking has potential applications beyond exceptions, including algebraic effect systems, resource management, and ownership tracking.
Summary updated