一句话:将流不敏感的 capability 机制扩展为流敏感,解耦能力生命周期与词法作用域,实现安全且表达力强的 typestate 编程。
在存在别名的情况下,安全且有表达力地管理有状态资源是编程语言的长期难题。作用域式构造(如 Java 的 synchronized 块)易于推理,却限制了表达力与并行性;命令式、流敏感的方法提供细粒度控制,但需要复杂的 typestate 分析,并迫使程序员显式跟踪状态。本文提出一种统一二者优点的方案:将传统的流不敏感 capability 机制扩展到流敏感场景,把能力生命周期与词法作用域解耦,使函数能够以流敏感方式接收、撤销或返回能力,并复用已有作用域能力机制来保证安全性与易用性。作者在 Scala 3 编译器中实现该扩展,借助路径依赖类型与隐式解析支持简洁、静态安全且富有表达力的 typestate 编程。原型可通用地支持文件操作、高级锁协议、DOM 构造与会话类型等多种模式,表明在已有 capability 支持的语言上仅需最小扩展即可实现表达力强且安全的 typestate 管理。
Managing stateful resources safely and expressively is a longstanding challenge in programming languages, especially in the presence of aliasing. For example, scope-based constructs like Java’s synchronized blocks offer ease of reasoning, but they restrict expressiveness and parallelism. Conversely, imperative, flow-sensitive approaches enable fine-grained control, but they require sophisticated typestate analyses and often burden programmers with explicit state tracking. In this work, we present a novel approach that unifies the ease of scoped reasoning with the expressiveness of imperative typestate management. Our design extends traditional flow-insensitive capability mechanisms to a flow-sensitive setting. In particular, we decouple capability lifetimes from lexical scopes, allowing functions to receive, revoke, or return capabilities in a flow-sensitive manner, building on existing mechanisms for the safety and ergonomics of scoped capability programming. We implement our approach as an extension to the Scala 3 compiler, leveraging path-dependent types and implicit resolution to enable concise, statically safe, and expressive typestate programming. Our prototype generically supports a wide range of patterns, including file operations, advanced locking protocols, DOM construction, and session types, showing that expressive and safe typestate management can be achieved with minimal extensions to an existing language with capability support.
@article{paperbot3765,
title = {Typestate via Revocable Capabilities},
author = {Songlin Jia and Craig Liu and Siyuan He and Haotian Deng and Yuyan Bao and Tiark Rompf},
journal = {Proceedings of the ACM on Programming Languages},
volume = {10},
number = {PLDI},
year = {2026},
doi = {10.1145/3808323}
}