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

JShell does not accept underscore from JEP 443 even with --enable-preview

    XMLWordPrintable

Details

    • b20
    • generic
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10 22H2 (OS Build 19045.3324), JDK 21 (Oracle Corporation 21+35-2513)
      jshell --enable-preview

      A DESCRIPTION OF THE PROBLEM :
      JEP 443 allows using an underscore (_) as an unnamed pattern when matching a record or for an unnamed exception, loop variable, resource in try-with-resources, etc.

      However, JShell will not accept a line with an identifier of a single underscore. For instance, when entering this code:

      int parseIntOrDefault(String value, int radix, int dflt) {
          try {
              return Integer.parseInt(value, radix);
          } catch (NumberFormatException _) {

      At this point, JShell prints:

      | Error:
      | reached end of file while parsing

      and returns to the prompt. If the method is entered using, say, e as the name of the variable for the exception, JShell will accept it and it is then possible to up arrow, go back, replace 'e' with '_', and save the function. Something in parsing the code will not accept underscore line-by-line in incomplete code, but will accept it for a complete method or class.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Attempt to type the code below into JShell (started by jshell --enable-preview).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code is accepted and the method is created.
      ACTUAL -
      | Error:
      | reached end of file while parsing

      ---------- BEGIN SOURCE ----------
      int parseIntOrDefault(String value, int radix, int dflt) {
          try {
              return Integer.parseInt(value, radix);
          } catch (NumberFormatException _) {
              return dflt;
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use an identifier, complete entering the code, then go back with up arrow or /edit and modify it to use underscore, the unnamed pattern.

      FREQUENCY : always


      Attachments

        Issue Links

          Activity

            People

              abimpoudis Angelos Bimpoudis
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: