Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8187694

Add compiler support for local variable type-inference

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 10
    • tools
    • None
    • source
    • low
    • Hide
      When using "-source 10" or later, declarations of classes, interfaces, or type variables named 'var' will no longer compile. References to such types in any context will also no longer compile (even if the declaration was compiled at a different source level), unless 'var' is re-interpreted to indicate an inferred type. These limitations are expected to be inobtrusive, because naming conventions discourage type names that begin with lowercase letters. Names of packages, variables, and methods are unaffected. Code compiled with "-source 9" or earlier is unaffected.
      Show
      When using "-source 10" or later, declarations of classes, interfaces, or type variables named 'var' will no longer compile. References to such types in any context will also no longer compile (even if the declaration was compiled at a different source level), unless 'var' is re-interpreted to indicate an inferred type. These limitations are expected to be inobtrusive, because naming conventions discourage type names that begin with lowercase letters. Names of packages, variables, and methods are unaffected. Code compiled with "-source 9" or earlier is unaffected.
    • Language construct
    • SE

      Summary

      Enhance the Java Language to extend type inference to declarations of local variables with initializers.

      Problem

      Declarations of the types of local variables are often unnecessary. See JEP 286 for an extended discussion.

      Solution

      The identifier var is removed from the domain of valid type names, and re-interpreted in local variable declarations, for loops, and try-with-resources statements as an indication that the type of the variable should be inferred.

      Inference determines the type of the variable's initializer, as if it were a standalone expression. A sanitization step removes any capture variables from the type by performing an upward projection to a supertype. The result is the type of the variable.

      An error occurs if the initializer has the null type, or is a poly expression that requires a target type. An error also occurs if the declaration has an unsupported form (such as a local variable declaration with trailing array dimensions).

      Specification

      Proposed changes to the Java Language Specification are attached.

            dlsmith Dan Smith
            mcimadamore Maurizio Cimadamore
            Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: