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

Putting key on generic Map fails when using CompileStatic

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.0.0-alpha-2, 2.4.15, 2.5.0
    • 4.0.0-beta-1, 3.0.9, 2.5.16
    • Static compilation
    • Elementary OS Freya 0.3.2
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

    Description

      I'm trying to use Groovy's short hand way to put a new key value to an encapsulated Map. But the compilation fails with:

       

      [Static type checking] - Cannot assign value of type java.lang.String to variable of type ?   
      

       

      Scenario to reproduce:

      import groovy.transform.CompileStatic
      
      @CompileStatic
      class MapHolder {
         private Map<?, ?> map = [:]
      
         Map<?, ?> getMap() {
            return map
         }
      }
      
      @CompileStatic
      static setMapValue() {
         MapHolder mapHolder = new MapHolder()
         mapHolder.map.key = 'value'
      }
      
      setMapValue()
      

      Using the put method works just fine:

      import groovy.transform.CompileStatic
      
      @CompileStatic
      class MapHolder {
         private Map<?, ?> map = [:]
      
         Map<?, ?> getMap() {
            return map
         }
      }
      
      @CompileStatic
      static setMapValue() {
         MapHolder mapHolder = new MapHolder()
         mapHolder.put('key', 'value')
      }
      
      setMapValue()
      

       

      Attachments

        Activity

          People

            emilles Eric Milles
            cleydsonjr Cleydson José de Figueiredo Júnior
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: