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...
翻译:许多面向目标的动态语言允许程序员从对象中提取方法,并将之作为函数处理。这允许灵活的编程模式,但给类型系统带来挑战。特别是,对方法提取的简单处理要求方法在接收器类型中具有反向性,从而压倒一切但不可能。我们对这一问题进行了详细调查,并提出了一种实施和评估的解决办法。方法提取是许多动态类型和逐步类型语言的特点,从Python和PHP到流程和类型Script。在这些语言中,可以访问作为程序记录的对象的基本表述,而执行方法的特性可以被重新确定为可独立调用的职能。在许多这类语言中,程序员可以明确指定在要求方法实施时使用的这一价值。不幸的是,正如我们所显示的那样,在方法提取过程中,像 TypeScript 和流动这样的渐进类型系统是不可靠的。输入任何这种系统的问题在于,它允许的灵活性必须被调制成一个像系统之间的连接,而执行方法的特性可以被重新配置为函数。在类中,在类型中,提取方法的类型和排序中, 和排序中, 将生成一种基本方法的排序中, 将生成到排序到排序到排序中, 将生成到排序到排序中, 将生成到排序中, 将产生一种方法到排序到排序到排序到排序到排序到排序到排序到排序到排序到排序到排序到排序中, 。