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/05/25 21:01:00 UTC
[jira] [Commented] (GROOVY-10102) Type checking fails when calling
trait method using indirect SelfType relation
[ https://issues.apache.org/jira/browse/GROOVY-10102?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17351352#comment-17351352 ]
Eric Milles commented on GROOVY-10102:
--------------------------------------
I think the intent of {{groovy.transform.SelfType}} is to indicate which non-trait classes your trait implementer is expected to descend from. Like {{@SelfType(Number)}} would indicate that a trait will be applied to number types. Therefore you gain access to the methods from Number in your trait under STC. "... but that the trait cannot extend that type itself" is the key phrase from the javadoc.
Instead of "@SelfType(A) trait B", you can combine using "trait B extends A" or "trait B implements A".
> Type checking fails when calling trait method using indirect SelfType relation
> ------------------------------------------------------------------------------
>
> Key: GROOVY-10102
> URL: https://issues.apache.org/jira/browse/GROOVY-10102
> Project: Groovy
> Issue Type: Bug
> Components: Compiler, Static Type Checker
> Affects Versions: 2.5.14
> Reporter: Renato Athaydes
> Assignee: Eric Milles
> Priority: Major
>
> I am having trouble migrating a very large project from Groovy 2.4.x to 2.5.14.
> I was able to reproduce the problem with this small example:
>
> {code:java}
> import groovy.transform.CompileStatic
> import groovy.transform.SelfType
> import java.util.function.Supplier
> @CompileStatic
> @SelfType([C])
> trait D {
> def go() {
> String s = foo
> withObject(s) {
> s.toUpperCase()
> }
> }
> }
> @SelfType(B)
> @CompileStatic
> trait C {}
> @SelfType([A])
> @CompileStatic
> trait B {}
> @CompileStatic
> trait A {
> String foo = 'foo'
> def withObject(String s, Supplier<Object> g) {
> s + g.get()
> }
> }
> class Impl implements A, B, C, D {
> def go() {
> println withObject(foo) { ' bar' }
> }
> }
> {code}
>
>
> Compiling this code results in this error:
>
> {noformat}
> src/main/groovy/bar/Foo.groovy: 13: [Static type checking] - Cannot find matching method <UnionType:bar.C+bar.D>#withObject(java.lang.String, groovy.lang.Closure). Please check if the declared type is correct and if the method exists.
> @ line 13, column 9.
> withObject(s) {
> ^
> 1 error
> {noformat}
> It's easy to work around once you know how: just make the trait that calls `withObject` list the trait that provides that method in its `SelfType` directly, i.e. in this case:
>
> {code:java}
> @SelfType([A, C])
> trait D { ... }
> {code}
>
> _Interestingly, changing D's self-type to `B` also works! It looks like the 2nd indirection is needed to reproduce the issue._
>
> However, figuring it out can take a long time! Would be nice to make this work automatically, maybe by just making `@SelfType(C)` equivalent to `@SelfType(C, B, A)` (i.e. merge the types by recursively applying other's self-types).
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)