一句话:提出支持可扩展变体、深层模式匹配、方法重写、穷尽性保证与独立类型检查的 trait 语言,实现模块化类型安全。
Traits 支持共享行为的组合与代码复用,Scala 中广泛用于 DSL 编译器;但模式匹配可扩展数据类型时,类型检查通过的程序仍可能因非穷尽匹配在运行时抛出异常。本文提出一种新的 trait 语言,首次同时支持可扩展数据类型、深层模式匹配、方法重写、穷尽性保证与分离类型检查,前三点服务 DSL 分析与优化,后两点支持可靠且可扩展的大型软件开发。方法上,作者在 Boolean-algebraic subtyping 框架中形式化该语言,并说明核心思想可迁移到其他类型系统。该设计使 Scala 等同时具有 trait 与可扩展变体的语言能够实现类型安全,改善 DSL 编译等场景的开发体验。
Traits provide a powerful mechanism for code reuse, as they allow the definition of shared behaviors that can be composed into classes. Scala traits in particular have been used extensively in both academia and industry to help define reusable components, especially in the context of domain-specific language (DSL) compilers. Pattern matching on the extensible data types representing a DSL’s constructs plays a key role in these applications. However, guaranteeing static type safety in this context is challenging: in Scala, a program using traits may successfully type check but then throw a runtime exception due to non-exhaustive pattern matching. This paper proposes a novel trait language which, for the first time, combines several important features: extensible data types, deep pattern matching, method overriding, exhaustiveness guarantees, and separate type checking. The former three are crucial to supporting DSL analysis and optimization use cases, while the latter two are important for reliable and scalable software development in the large. We formalize our approach in the framework of Boolean-algebraic subtyping, but its core ideas could be adapted to other type systems; thanks to it, languages like Scala that feature traits and extensible variants can finally become type safe, improving the experience of developers working with DSL compilation and related use cases.
@article{paperbot12085,
title = {Modular Type Safety for Traits with Extensible Variants and Deep Pattern Matching},
author = {Andong Fan and Lionel Parreaux and Ningning Xie},
journal = {Proceedings of the ACM on Programming Languages},
volume = {10},
number = {OOPSLA2},
year = {2026},
doi = {10.1145/3839514}
}