Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4683 static import collection task
  3. GROOVY-4483

Constants resolving doesn't work in map.put()

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.7.3, 1.7.4, 1.7.5
    • 1.8.7
    • Compiler
    • None
    • Groovy 1.7.3

    Description

      I have the following code which doesn't work:

      import static Constants.*
      
      class Constants {
        static final String NAME = "name"
      }
      
      def map = [:]
      if(!map[NAME]) {               // <------ this succeeds...
        map[NAME] = "erik"          // <------ this fails!
      }
      
      println map
      

      However, the following does work:

      import static Constants.*
      
      class Constants {
        public static final String NAME = "name"         // <---- mind the public!
      }
      
      def map = [:]
      if(!map[NAME]) {             // <------ this succeeds...
        map[NAME] = "erik"          // <------ and this also succeeds...
      }
      

      This can also be solved by prefixing the NAME constant with the class (Constants.NAME)

      Attachments

        Activity

          People

            Unassigned Unassigned
            bodiam Erik Pragt
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: