skip to main content
research-article

Java type inference is broken: can we fix it?

Published:19 October 2008Publication History
Skip Abstract Section

Abstract

Java 5, the most recent major update to the Java Programming Language, introduced a number of sophisticated features, including a major extension to the type system. While the technical details of these new features are complex, much of this complexity is hidden from the typical Java developer by an ambitious type inference mechanism. Unfortunately, the extensions to the Java 5 type system were so novel that their technical details had not yet been thoroughly investigated in the research literature. As a result, the Java 5 compiler includes a pragmatic but flawed type inference algorithm that is, by design, neither sound nor locally complete. The language specification points out that neither of these failures is catastrophic: the correctness of potentially-unsound results must be verified during type checking; and incompleteness can usually be worked around by manually providing the method type parameter bindings for a given call site.

This paper dissects the type inference algorithm of Java 5 and proposes a signficant revision that is sound and able to calculate correct results where the Java 5 algorithm fails. The new algorithm is locally complete with the exception of a difficult corner case. Moreover, the new algorithm demonstrates that several arbitrary restrictions in the Java type system---most notably the ban on lower-bounded type parameter declarations and the limited expressibility of intersection types---are unnecessary. We hope that this work will spur the evolution of a more coherent, more comprehensive generic type system for Java.

References

  1. Gilad Bracha, Martin Odersky, David Stoutamire, & Philip Wadler. Making the Future Safe for the Past: Adding Genericity to the Java Programming Language. OOPSLA, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Luca Cardelli. An Implementation of F:. Research report 97, DEC Systems Research Center, 1993.Google ScholarGoogle Scholar
  3. James Gosling, Bill Joy, Guy Steele, & Gilad Bracha. The Java Language Specification, Third Edition. 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Atshushi Igarashi, Benjamin Pierce, & Philip Wadler. Featherweight Java: A Minimal Core Calculus for Java and GJ. OOPSLA, 1999. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Atsushi Igarashi & Mirko Viroli. On Variance-Based Subtyping for Parameteric Types. ECOOP, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Atsushi Igarashi & Hideshi Nagira. Union Types for Object-Oriented Programming. Journal of Object Technology, vol. 6, no. 2, February 2007.Google ScholarGoogle Scholar
  7. Andrew J. Kennedy & Benjamin C. Pierce. On Decidability of Nominal Subtyping with Variance. FOOL/WOOD, 2007.Google ScholarGoogle Scholar
  8. Benjamin C. Pierce & David N. Turner. Local Type Argument Synthesis with Bounded Quantification. Technical report TR495, Indiana University, 1997.Google ScholarGoogle Scholar
  9. Benjamin C. Pierce & David N. Turner. Local Type Inference. POPL, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Daniel Smith. Completing the Java Type System. Master's thesis, Rice University, 2007.Google ScholarGoogle Scholar
  11. Kresten Krab Thorup & Mads Torgersen. Unifying Genericity: Combining the Benefits of Virtual Types and Parameterized Classes. Lecture Notes in Computer Science, 1999.Google ScholarGoogle Scholar
  12. Mads Torgersen, Christian Plesner Hansen, Erik Ernst, Peter von der Ahé, Gilad Bracha, & Neal Gafter. Adding Wildcards to the Java Programming Language. SAC, 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Mads Torgersen, Erik Ernst, & Christian Plesner Hansen. Wild FJ. FOOL, 2005.Google ScholarGoogle Scholar
  14. DrJava IDE. http://drjava.org.Google ScholarGoogle Scholar
  15. Java Community Process. http://jcp.org.Google ScholarGoogle Scholar
  16. "Type variables should have lower/super bounds." Java Request for Enhancement. http://bugs.sun.com/view_bug.do?bug_id=5052956.Google ScholarGoogle Scholar
  17. "Please introduce a name for the "null' type." Java Request for Enhancement. http://bugs.sun.com/view_bug.do?bug_id=5060259.Google ScholarGoogle Scholar
  18. "Multiply-bounded reference type expressions." Java Request for Enhancement. http://bugs.sun.com/view_bug.do?bug_id=6350706.Google ScholarGoogle Scholar

Index Terms

  1. Java type inference is broken: can we fix it?

            Recommendations

            Comments

            Login options

            Check if you have access through your login credentials or your institution to get full access on this article.

            Sign in

            Full Access

            PDF Format

            View or Download as a PDF file.

            PDF

            eReader

            View online with eReader.

            eReader
            About Cookies On This Site

            We use cookies to ensure that we give you the best experience on our website.

            Learn more

            Got it!