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

Type inference breaks for Collection.inject

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 2.4.6
    • None
    • None

    Description

      The code below should not compile as the first parameter to the inject method must be a string, but the integer 0 is handed in:

      @CompileStatic
      public static void main(String[] args) {
          def inList = ["a", "b", "c"]
          def outList = inList.inject(0, { a, b -> a + "," + b })
          println(outList)
      }
      

      I opened this issue as adviced in the reply to the same issue I asked on stackoverflow: http://stackoverflow.com/questions/39444838/chance-for-this-hole-in-groovy-staic-typing-to-be-fixed/39446458#39446458

      Java streams example (from link):

      var list = java.util.Arrays.asList("a", "b", "c");
      var out = list.stream().reduce(0, (x,y) -> x + y);
      System.out.println(out);
      

      NOTE: You can use java streams apis in groovy:

      @groovy.transform.CompileStatic
      void test() {
        def list = ['a','b','c']
        def str = list.stream().reduce("", (x,y) -> x + y) // STC error if "" is 0
        println str
      }
      test()
      

      Attachments

        1. screenshot-1.png
          17 kB
          Eric Milles

        Issue Links

          Activity

            People

              emilles Eric Milles
              OlliP Oliver Holp
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: