论文标题
类型检查提取的方法
Type Checking Extracted Methods
论文作者
论文摘要
许多面向对象的动态语言允许程序员从对象中攻击方法_并将其视为函数。这允许灵活的编程模式,但对类型系统提出了挑战。特别是,对方法提取的简单处理将需要方法在接收器类型中是违反的,从而使全部但不可能。我们介绍了对此问题的详细研究,以及实施和评估的解决方案。方法提取是许多动态投入和逐渐型的语言的特征,从Python和PHP到Flow and Typescript。在这些语言中,可以访问对象作为过程记录的基础表示,并且可以将实现方法的过程重新列为可以独立调用的函数。在许多此类语言中,程序员可以明确指定在调用方法实现时要使用的“此”值。不幸的是,正如我们所表明的那样,在方法提取的情况下,现有的渐进类型系统(例如打字稿和流程)不符合。键入任何此类系统的问题是,必须通过从方法中提取的对象与后来调用的函数值之间进行连接来驯服其允许的灵活性。在球拍中,其中一种被称为“结构类型属性”的类似方法提取的设施是类,通用方法和其他API的基础,因此出现了这些相同的挑战,必须解决以支持类型的球拍中的此功能。我们展示了如何结合两个现有类型的系统功能 - 存在类型和发生键入 - 以产生键入方法提取的合理方法。我们将设计正式化,扩展了类型的球拍类型系统的现有正式模型,并证明了我们的扩展是合理的。我们的设计也可以在发布的“球拍”版本中实现,并且与所有现有的打字球拍软件包兼容,其中许多已经使用了此功能的先前版本。
Many object-oriented dynamic languages allow programmers to _extract methods_ from objects and treat them as functions. This allows for flexible programming patterns, but presents challenges for type systems. In particular, a simple treatment of method extraction would require methods to be contravariant in the receiver type, making overriding all-but-impossible. We present a detailed investigation of this problem, as well as an implemented and evaluated solution. Method extraction is a feature of many dynamically-typed and gradually-typed languages, ranging from Python and PHP to Flow and TypeScript. In these languages, the underlying representation of objects as records of procedures can be accessed, and the procedures that implement methods can be reified as functions that can be called independently. In many of these languages, the programmer can then explicitly specify the `this` value to be used when the method implementation is called. Unfortunately, as we show, existing gradual type systems such as TypeScript and Flow are unsound in the presence of method extraction. The problem for typing any such system is that the flexibility it allows must be tamed by requiring a connection between the object the method was extracted from, and the function value that is later called. In Racket, where a method extraction-like facility, dubbed "structure type properties", is fundamental to classes, generic methods, and other APIs, these same challenges arise, and must be solved to support this feature in Typed Racket. We show how to combine two existing type system features -- existential types and occurrence typing -- to produce a sound approach to typing method extraction. We formalize our design, extending an existing formal model of the Typed Racket type system, and prove that our extension is sound. Our design is also implemented in the released version of Racket, and is compatible with all existing Typed Racket packages, many of which already used a previous version of this feature.