Type Checking Extracted Methods

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 contravari...

Full description

Bibliographic Details
Main Authors: Fu, Y. (Author), Tobin-Hochstadt, S. (Author)
Format: Article
Language:English
Published: AOSA Inc. 2022
Subjects:
Online Access:View Fulltext in Publisher
LEADER 02629nam a2200181Ia 4500
001 10.22152-programming-journal.org-2022-6-6
008 220425s2022 CNT 000 0 und d
020 |a 24737321 (ISSN) 
245 1 0 |a Type Checking Extracted Methods 
260 0 |b AOSA Inc.  |c 2022 
856 |z View Fulltext in Publisher  |u https://doi.org/10.22152/programming-journal.org/2022/6/6 
520 3 |a 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. © Yuquan Fu and Sam Tobin-Hochstadt. 
650 0 4 |a gradual typing 
650 0 4 |a methods 
650 0 4 |a types 
700 1 |a Fu, Y.  |e author 
700 1 |a Tobin-Hochstadt, S.  |e author 
773 |t Art, Science, and Engineering of Programming