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 2022/11/04 18:34:00 UTC
[jira] [Resolved] (GROOVY-10807) STC seems to lose type information through method references
[ https://issues.apache.org/jira/browse/GROOVY-10807?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Eric Milles resolved GROOVY-10807.
----------------------------------
Resolution: Fixed
https://github.com/apache/groovy/commit/7e3091985f72c6dd1350259bdf189cc231c2a642
> STC seems to lose type information through method references
> ------------------------------------------------------------
>
> Key: GROOVY-10807
> URL: https://issues.apache.org/jira/browse/GROOVY-10807
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 3.0.12, 3.0.13, 4.0.6
> Reporter: Sterling Greene
> Assignee: Eric Milles
> Priority: Major
>
> This compiles in Groovy 3.0.11, but it does not compile in Groovy 3.0.12 or 3.0.13.
> {code:java}
> import groovy.transform.CompileStatic
> @CompileStatic
> abstract class NoticeRenderer {
> private static final Comparator<String> COMPARATOR = Comparator<String>.comparing(NoticeRenderer::getComponentDisplayName)
>
> public static String getComponentDisplayName(String component) {
> return component;
> }
> }
> {code}
> The error is
> bq. Failed to find the expected method[getComponentDisplayName(java.lang.Object)] in the type[NoticeRenderer] at line: 5, column: 79
> I think something is going sideways when inferring the types to comparing.
> Removing the type bound on the Comparator doesn't change the error.
> bq. private static final Comparator<String> COMPARATOR = Comparator.comparing(NoticeRenderer::getComponentDisplayName)
> The type bound could be on the method itself. This produces the same error.
> bq. private static final Comparator<String> COMPARATOR = Comparator.<String>comparing(NoticeRenderer::getComponentDisplayName)
> But comparing actually has two type parameters, so it's a little weird that the above doesn't complain. Specifying both type parameters still produces the same error.
> bq. private static final Comparator<String> COMPARATOR = Comparator.<String, String>comparing(NoticeRenderer::getComponentDisplayName)
> If I mistakenly put the type bounds on Comparator, I don't expect this to work, but it produces BUG! output:
> private static final Comparator<String> COMPARATOR = Comparator<String, String>.comparing(NoticeRenderer::getComponentDisplayName)
> bq. BUG! exception in phase 'instruction selection' in source unit 'ConsoleScript34' Expected earlier checking to detect generics parameter arity mismatch
> bq. Expected: java.util.Comparator<T>
> bq. Supplied: java.util.Comparator<java.lang.String,java.lang.String>
> In Java, you don't need the type information at all:
> bq. private static final Comparator<String> COMPARATOR = Comparator.comparing(NoticeRenderer::getComponentDisplayName);
> If you wanted to be verbose in Java, you could specify the bounds:
> bq. private static final Comparator<String> COMPARATOR = Comparator.<String, String>comparing(NoticeRenderer::getComponentDisplayName);
> As a workaround...
> If I avoid the use of the method reference, I can make something pass the static compiler:
> bq. private static final Comparator<String> COMPARATOR = Comparator.<String>comparing( { String it -> getComponentDisplayName(it) })
> I need both the type bound on comparing and the type of the parameter.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)