You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/12/20 14:00:00 UTC
[jira] [Updated] (GROOVY-7934) Type inference breaks for Collection.inject
[ https://issues.apache.org/jira/browse/GROOVY-7934?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Eric Milles updated GROOVY-7934:
--------------------------------
Attachment: screenshot-1.png
> Type inference breaks for Collection.inject
> -------------------------------------------
>
> Key: GROOVY-7934
> URL: https://issues.apache.org/jira/browse/GROOVY-7934
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 2.4.6
> Reporter: Oliver Holp
> Assignee: Eric Milles
> Priority: Minor
> Labels: CompileStatic
> Attachments: screenshot-1.png
>
>
> 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:
> {code:groovy}
> @CompileStatic
> public static void main(String[] args) {
> def inList = ["a", "b", "c"]
> def outList = inList.inject(0, { a, b -> a + "," + b })
> println(outList)
> }
> {code}
> 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):
> {code:java}
> var list = java.util.Arrays.asList("a", "b", "c");
> var out = list.stream().reduce(0, (x,y) -> x + y);
> System.out.println(out);
> {code}
> NOTE: You can use java streams apis in groovy:
> {code: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()
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)