You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "mgroovy (Jira)" <ji...@apache.org> on 2021/03/06 00:21:00 UTC

[jira] [Comment Edited] (GROOVY-9968) @TypeChecked Error: Members of Iterable are Foo, not Object

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

mgroovy edited comment on GROOVY-9968 at 3/6/21, 12:20 AM:
-----------------------------------------------------------

* Yes, exactly.
* The error also occurs when saying e.g.
{code:java}
final cols = primaryKeyColumns(table) 
//final Columns<Column> cols = primaryKeyColumns(table) // this works
cols.collect { Column c -> c.name } // ERRs: expected Object, got Column & c does not have member name
{code}
* Column class has name member
* primaryKeyColumns method has return type Columns (also without a diamond)
* What I also find confusing is, that the compiler complains about two things:
## That he expected Object but got Column
## That c (of type Column) does not have a name member
* I would find it more clear if only the first error appeared, the 2nd one is just clutter...


was (Author: emge):
* Yes, exactly.
* The error also occurs when saying e.g. (Note: Column class has name member):
{code:java}
final cols = primaryKeyColumns(table) 
//final Columns<Column> cols = primaryKeyColumns(table) // this works
cols.collect { Column c -> c.name } // ERRs: expected Object, got Column & c does not have member name
{code}
* What I also find confusing is, that the compiler complains about two things:
## That he expected Object but got Column
## That c (of type Column) does not have a name member
* I would find it more clear if only the first error appeared, the 2nd one is just clutter...

> @TypeChecked Error: Members of Iterable<T extends Foo> are Foo, not Object
> --------------------------------------------------------------------------
>
>                 Key: GROOVY-9968
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9968
>             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
>            Assignee: Eric Milles
>            Priority: Major
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> *Problem*
> Trying to build the sample test code below fails with:
> {code:java}
> Groovyc: [Static type checking] - No such property: name for class: java.lang.Object
> Groovyc: Expected parameter of type java.lang.Object but got simple.groovy.bugs.groovy3.gb_2021_03_05.iterable_t_extends.Foo
> Groovyc: [Static type checking] - No such property: name for class: java.lang.Object
> {code}
> If we do not explicitly give the member type of the IterableTExtendsFoo iterable, Groovy assumes that the type is Object, even though it has been defined as being a subclass of Foo in the definition of IterableTExtendsFoo<T extends Foo>.
> *Expected*
>  * Code should compile.
>  * If Groovy could not deduce that IterableTExtendsFoo has been initialized with a List<T extends Foo>, then it should reject the line where the IterableTExtendsFoo ctor is called.
>  * Note: Works as expected in Groovy 2.5.x (at least up to 2.5.14)
> *Sample Code*
> {code:java}
> import groovy.transform.TypeChecked
> import org.junit.Ignore
> import org.junit.Test
> @TypeChecked
> class Groovy3_Iterable_T_extends_Bug {
> 	@Test
> 	@Ignore
> 	void 'Groovy 3-0-7 Members of Iterable T extends Foo are Foo'() {
> 		// Compiler should reject the following line if the ctor arg would not of type List<T extends Foo>
> 		final iterableTExtendsFoo = new IterableTExtendsFoo([new Foo('Sli'),new Foo('Msha'),new Foo('Dy'),])
> 		//final iterableTExtendsFoo = new IterableTExtendsFoo<Foo>([new Foo('Sli'),new Foo('Msha'),new Foo('Dy'),]) // Explicitly giving the iterable member type makes the code compile
> 		println iterableTExtendsFoo.collect { "Hi, my name is: $it.name" }
> 		println iterableTExtendsFoo.collect { Foo f -> "name=$f.name" }
> 	}
> }
> {code}
> {code:java}
> @Canonical class Foo { String name }
> {code}
> {code:java}
> @Canonical
> class IterableTExtendsFoo<T extends Foo> implements Iterable<T> {
> 	List<T> foos
> 	@Override Iterator<T> iterator() { foos.iterator() }
> }
> {code}



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