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

Class.isCase() documentation does not clarify that Class.isCase(SomeClass) is false, or why

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.0.8, 4.0.0-alpha-3
    • 4.0.0-beta-1, 3.0.9
    • None
    • Pop!_OS 20.04 LTS
      Linux Kernel 5.11.0-7614-generic

    Description

      From the documentation for Groovy's java.lang.Class extensions:

      public boolean isCase(Object switchValue)

      Special 'Case' implementation for Class, which allows testing for a certain class in a switch statement. For example:

       

      switch( obj ) {
        case List :
          // obj is a list
          break;
        case Set :
          // etc
      }

       

      Reading that, a person might reasonably be confused by the following behavior:

      class SomeClass {}
      
      switch (SomeClass) {
          case Class:
              // we expect to go here
              println("case Class")
              break
          /* ... more cases here, presumably ... */
          default:
              // but we end up here?
              println("default")
              break
      }
      

      Looking at the code, I believe this is intentional - because TestClass is itself a Class, isCase() uses isAssignableFrom() rather than the expected isInstance(), and Class is not assignable from the unrelated SomeClass type. Since this case is ambiguous, the code clearly cannot do both things, and we shouldn't change the existing behavior in case any applications depend on it. However, we could provide better documentation so it's clear that using this with a Class will not work as expected.

      Attachments

        Activity

          People

            paulk Paul King
            curtis.mackie Curtis Mackie
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: