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

Type inference fails for methods returning generic map types

    XMLWordPrintableJSON

Details

    Description

      The static type checker rejects the last of the 4 assignments in the following code:

      @groovy.transform.CompileStatic
      class Foo {
          static void main(String... args) {
              List<Integer> list = [1, 2, 3]
              List<Integer> immutableList = [1, 2, 3].asImmutable()
              Map<String, Integer> map = [foo: 123, bar: 456]
              Map<String, Integer> immutableMap = [foo: 123, bar: 456].asImmutable()
          }
      }
      

      with the error Incompatible generic argument types. Cannot assign java.util.Map <java.lang.Object, java.lang.Object> to: java.util.Map <String, Integer>.

      The generic signature on asImmutable should be sufficient for type inference to work (as it does for the list in the 2nd assignment).

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            robfletcher Rob Fletcher
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: