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

Add compiler support for local variable type-inference

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Approved
    • Fix Version/s: 10
    • Component/s: tools
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      source
    • Compatibility Risk:
      low
    • Compatibility Risk Description:
      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.
    • Interface Kind:
      Language construct
    • Scope:
      SE

      Description

      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.

        Attachments

        1. local-var-inference.html
          77 kB
          Dan Smith
        2. local-var-inference.html
          77 kB
          Dan Smith
        3. local-var-inference.html
          75 kB
          Dan Smith

          Issue Links

            Activity

              People

              • Assignee:
                dlsmith Dan Smith
                Reporter:
                mcimadamore Maurizio Cimadamore
                Reviewed By:
                Maurizio Cimadamore
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: