一句话:提出在无函数名重载的函数式语言中借助参数多态与 Hindley-Milner 类型推断实现编译期检查的 Fluent API。
Fluent API 是一种面向对象模式,用于构建优雅的 API 和嵌入式 DSL;智能 Fluent API 能在编译期强制 API 协议或 DSL 语法,但其实现通常依赖函数名重载,因此在函数式语言中难以实现。本文研究如何在缺少名字重载的函数式编程语言中实现 Fluent API,核心方法是利用参数多态和 Hindley-Milner 类型推断来编码协议约束。该实现能够支持正则语言类和确定性上下文无关语言类中的 Fluent API 协议,甚至可表达超出这些类别的协议,表明函数式语言同样可以实现具有编译期协议检查能力的 Fluent API 与嵌入式 DSL。
Fluent API is an object-oriented pattern for elegant APIs and embedded DSLs. A smart fluent API can enforce the API protocol or DSL syntax at compile time. Since fluent API implementations typically rely on overloading function names, they are hard to realize in functional programming languages. This work shows how functional fluent APIs can be implemented in the absence of name overloading, by relying on parametric polymorphism and Hindley-Milner type inference. The implementation supports fluent API protocols in the regular- and deterministic context-free language classes, and even beyond.