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 2021/03/08 15:09:00 UTC

[jira] [Comment Edited] (GROOVY-9971) @TypeChecked: Closure no longer compatible with Closure argument

    [ https://issues.apache.org/jira/browse/GROOVY-9971?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17297437#comment-17297437 ] 

Eric Milles edited comment on GROOVY-9971 at 3/8/21, 3:08 PM:
--------------------------------------------------------------

I'm not sure what can be done here.  The generics check is accurate in that {{Type<GString>}} is not compatible with {{Type<String>}}.  If the method accepting the closure was a Java method, there would be not chance for conversion of the return value of the closure.

I have seen test cases like {{List<String> list = ["$x"]}}.  When this assignment was allowed to succeed, the caller of {{list[0]}} would unexpectedly get a {{GString}} instance, not a {{String}}.

You can change your method to accept {{Closure<? extends CharSequence>}} or {{Closure<?>}} and always take the {{toString()}} of the return value.


was (Author: emilles):
I'm not sure what can be done here.  The generics check is accurate in that {{Type<GString>}} is not compatible with {{Type<String>}}.  If the method accepting the closure was a Java method, there would be not chance for conversion of the return value of the closure.

I have seen test cases like {{List<String> list = ["$x"]}}.  When this assignment was allowed to succeed, the caller of {{list[0]}} would unexpectedly get a {{GString}} instance, not a {{String}}.

You can change your method to accept {{Closure<? extends CharSequence>}} or {{Closure<?>}} and always take the {{toString}} of the return value.

> @TypeChecked: Closure<String> no longer compatible with Closure<GString> argument
> ---------------------------------------------------------------------------------
>
>                 Key: GROOVY-9971
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9971
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 3.0.7
>         Environment: Windows 10
> jdk-11.0.10.9-hotspot
> IntelliJ 2020.3.2
>            Reporter: mgroovy
>            Priority: Major
>
> *Problem*
> A Closure<String> is in Groovy 3 no longer compatible with a closure that returns a GString (or String|GString), even though GString is autoconverted
> to String in Groovy.
> *Expected*
> A Closure returning a GString or a String|GString should be compatible with Closure<String>
> *Sample Code*
> {code:java}
> import groovy.transform.TypeChecked
> import org.junit.Ignore
> import org.junit.Test
> @TypeChecked
> class Groovy3_ClosureOfString_GString_Compatible {
> 	@Test @Ignore
> 	void 'Groovy 3-0-7 Closure with String return type not compatible with GString argument'() {
> 		final x = 123
> 		// Cannot find matching method execStringCls(Closure<groovy.lang.GString>)
> 		assert execStringCls { "x=$x" } == 'x=123'
> 		//assert execStringCls { "x=$x".toString() } == 'x=123'  // works
> 	}
> 	@Test @Ignore
> 	void 'Groovy 3-0-7 Closure with String return type not compatible with GString argument 2'() {
> 		final x = 123
> 		// Cannot find matching method execStringCls(Closure<java.io.Serializable>)
> 		assert execStringCls { false ? 'abc' : "x=$x" } == 'x=123'
> 		//assert execStringCls { true ? 'abc' : "x=$x".toString() } == 'x=123' // works
> 	}
> 	String execStringCls(Closure<String> cls) {
> 		cls()
> 	}
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)