A type-and-effect system for object initialization

Journal Article (2020)
Author(s)

Fengyun Liu (École Polytechnique Fédérale de Lausanne)

Ondřej Lhoták (University of Waterloo)

Aggelos Biboudis (École Polytechnique Fédérale de Lausanne)

Paolo G. Giarrusso (TU Delft - Programming Languages)

Martin Odersky (École Polytechnique Fédérale de Lausanne)

Research Group
Programming Languages
Copyright
© 2020 Fengyun Liu, Ondřej Lhoták, Aggelos Biboudis, P.G. Giarrusso, Martin Odersky
DOI related publication
https://doi.org/10.1145/3428243
More Info
expand_more
Publication Year
2020
Language
English
Copyright
© 2020 Fengyun Liu, Ondřej Lhoták, Aggelos Biboudis, P.G. Giarrusso, Martin Odersky
Research Group
Programming Languages
Issue number
OOPSLA
Volume number
4
Reuse Rights

Other than for strictly personal use, it is not permitted to download, forward or distribute the text or part of it, without the consent of the author(s) and/or copyright holder(s), unless the work is under an open content license such as Creative Commons.

Abstract

Every newly created object goes through several initialization states: starting from a state where all fields are uninitialized until all of them are assigned. Any operation on the object during its initialization process, which usually happens in the constructor via this, has to observe the initialization states of the object for correctness, i.e. only initialized fields may be used. Checking safe usage of this statically, without manual annotation of initialization states in the source code, is a challenge, due to aliasing and virtual method calls on this. Mainstream languages either do not check initialization errors, such as Java, C++, Scala, or they defend against them by not supporting useful initialization patterns, such as Swift. In parallel, past research has shown that safe initialization can be achieved for varying degrees of expressiveness but by sacrificing syntactic simplicity. We approach the problem by upholding local reasoning about initialization which avoids whole-program analysis, and we achieve typestate polymorphism via subtyping. On this basis, we put forward a novel type-and-effect system that can effectively ensure initialization safety while allowing flexible initialization patterns. We implement an initialization checker in the Scala 3 compiler and evaluate on several real-world projects.