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

Automatic type casting with switch..case with instanceof

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Duplicate
    • 3.0.2
    • None
    • Compiler
    • None

    Description

      It could be useful to have automatic type casting (also with static compilation) with switch..case with instanceof:

      void sortItOut(Object o) {
      
          switch(o) {
            case String:
              println "String: ${o.length()}" //currently only in dynamic Groovy
              break
            case Integer:
              println "Integer: ${o.longValue()}" //currently only in dynamic Groovy 
              break
            default:
              throw new IllegalArgumentException(o.toString())
          }
      }
      

      The same as currently for if with instanceof:

      if (o instanceof String) {
          useString(o)
      }
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              szpak Marcin Zajaczkowski
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: