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.
翻译:安全且富有表现力地管理有状态资源是编程语言领域的一个长期挑战,尤其是在存在别名的情况下。例如,像Java的同步块这样基于作用域的构造简化了推理,但限制了表达能力和并行性。相反,命令式、流敏感的方法实现了细粒度控制,但需要复杂的类型状态分析,并且常常给程序员带来显式状态追踪的负担。在这项工作中,我们提出了一种新颖的方法,统一了作用域推理的简便性与命令式类型状态管理的表达能力。我们的设计将传统的流不敏感能力机制扩展到流敏感环境。具体而言,我们将能力的生命周期与词法作用域解耦,允许函数以流敏感的方式接收、撤销或返回能力,并建立在现有用于作用域能力编程的安全性与人体工程学机制之上。我们将该方法实现为Scala 3编译器的扩展,利用路径依赖类型和隐式解析来实现简洁、静态安全且富有表现力的类型状态编程。我们的原型泛型支持多种模式,包括文件操作、高级锁定协议、DOM构建和会话类型,表明通过最小扩展现有支持能力的语言,即可实现富有表现力且安全的类型状态管理。