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

Underscore as parameter name in one-parameter functional types fails to compile

XMLWordPrintable

    • b05
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Compilation fails if yield statement is followed by underscore, even if it is legal syntax, like yield _ -> {}. This is issue that has been present in jdk since introduction of unnamed variables and patterns

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile following code:

      public class SwitchYieldAndUnderscore {
        public static void main(String[] args) {
          Consumer<String> value = switch (args[0]) {
            case "foo" -> {
              yield _ -> {};
            }
            default -> throw new AssertionError();
          };
        }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compilation successful
      ACTUAL -
      Compilation failes with 2 errors:
      ";" exprected
      "=" exprected

      ---------- BEGIN SOURCE ----------
      public class SwitchYieldAndUnderscore {
        public static void main(String[] args) {
          Consumer<String> value = switch (args[0]) {
            case "foo" -> {
              yield _ -> {};
            }
            default -> throw new AssertionError();
          };
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Enclose _ in paretheses like so: yield (_) -> {}

      FREQUENCY : always


            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: