Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-878

Allow Closures to be a switch case by implementing the isCase() method

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.0-JSR-1
    • 1.0-JSR-2
    • None
    • None

    Description

      implement isCase() on Closures like
      public boolean isCase(Object candidate)

      { return InvokerHelper.asBool(call(candidate)); }

      to let Closures play as switch cases like in SwitchTest

      void testSwitchWithClosure(){
      switch(0){
      case

      {true}

      : break
      default: assert false
      }
      switch(0){
      case

      {false}

      : assert false
      }
      switch(0){
      case

      {it == 0}

      : break
      default: assert false
      }
      switch(0){
      case

      { candidate -> candidate == 0}

      : break
      default: assert false
      }
      }

      That way, we can switch on logic.

      Attachments

        Activity

          People

            codevise Dierk König
            codevise Dierk König
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: