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
Attachments
Issue Links
- is related to
-
GROOVY-5893 Return type of "inject" is not inferred properly
- Closed
-
GROOVY-6504 Type checker select the wrong inject method
- Closed
-
GROOVY-5924 Provide a mechanism to allow the type checker to know what are the expected argument types of a closure
- Closed