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

flow typing activated by if statement doing an "instanceof" check doesn't work as expected with interface types

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.2, 2.4.0-rc-1
    • 2.4.6
    • Static Type Checker
    • None

    Description

      The following code example fails with this STC error in compilation:

      1 compilation error:
      
      [Static type checking] - Cannot call java.util.Set <CharSequence>#add(java.lang.CharSequence) with arguments [<UnionTypejava.lang.Object+java.lang.CharSequence>] 
       at line: 10, column: 12
      
      @groovy.transform.CompileStatic
      class ShowUnionTypeBug {
          Map<String, Object> instanceMap = (Map<String,Object>)['a': 'Hello World']
          def findInstance(String key) {
              Set<CharSequence> allInstances = [] as Set
              def instance = instanceMap.get(key)
              if(instance instanceof CharSequence) {
                 allInstances.add(instance)
              }
              allInstances
          }
      }
      new ShowUnionTypeBug().findInstance('a')
      

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            paulk Paul King
            lhotari Lari Hotari
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment