Wujek Srujek | 20 Jul 2012 15:07
Picon

[groovy-user] make find, each and so on type-aware?

I have the following code:

def found = getAll().find { Person it -> it.login == login } as Person

In order for found to be inferred as Person, I must declare it as Person and cast the result to Person, otherwise type checking tells me that it is object. The problems seems to be that all such methods in DGM work with objects and return objects. Can this be somehow changed, or is it too late, impossible, unnecessary?

wujek
Cédric Champeau | 20 Jul 2012 15:18
Picon
Gravatar

Re: [groovy-user] make find, each and so on type-aware?

Le 20/07/2012 15:07, Wujek Srujek a écrit :
I have the following code:

def found = getAll().find { Person it -> it.login == login } as Person

In order for found to be inferred as Person, I must declare it as Person and cast the result to Person, otherwise type checking tells me that it is object. The problems seems to be that all such methods in DGM work with objects and return objects. Can this be somehow changed, or is it too late, impossible, unnecessary?

wujek
This is one of the currently discussed gotchas of type checking. The problem is that source code doesn't contain enough type information for the compiler to infer argument types. It is discussed here: http://groovy.329449.n5.nabble.com/Closure-parameter-types-inference-td4978037.html

-- Cédric Champeau SpringSource - A Division Of VMware http://www.springsource.com/ http://twitter.com/CedricChampeau
Ronny Løvtangen | 20 Jul 2012 15:41

Re: [groovy-user] make find, each and so on type-aware?

Does your getAll() return a Collection or Collection<Person>?
If the collection uses generics then 'as Person' shouldn't be necessary to please the IDE.
If not, then it depends on how good the IDE is to infer the types (if possible). IntelliJ does a pretty good job
here, given it can infer the type from where the collection is created. I guess it's because it knows
details about most of the Groovy collection API.

As an example, IntelliJ infers that 'it' and 'found' must be a Date in the following example:

		def list = [new Date()-1, new Date()+1]
		def found = list.find {it > new Date()}
		println found.time

But when it comes to passing around closures, it gets really complicated, as Cédric's link explains.

Ronny

On Jul 20, 2012, at 3:07 PM, Wujek Srujek wrote:

> I have the following code:
> 
> def found = getAll().find { Person it -> it.login == login } as Person
> 
> In order for found to be inferred as Person, I must declare it as Person and cast the result to Person,
otherwise type checking tells me that it is object. The problems seems to be that all such methods in DGM
work with objects and return objects. Can this be somehow changed, or is it too late, impossible, unnecessary?
> 
> wujek

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Wujek Srujek | 20 Jul 2012 15:49
Picon

Re: [groovy-user] make find, each and so on type-aware?

It returns Iterable<Person>. And I am using IntelliJ ;d It is smart most of the time, but that's not the problem here - I am using it with <at> TypeChecked and it results in an error (for example: 'Object has no attribute 'login'' or similar, or later, when I try to access found). True, if I don't use <at> TypeChecked, I get no warnings, but that's not the point here ;d


wujek

On Fri, Jul 20, 2012 at 3:41 PM, Ronny Løvtangen <lists-Zb5NtxTwiyqaMJb+Lgu22Q@public.gmane.org> wrote:
Does your getAll() return a Collection or Collection<Person>?
If the collection uses generics then 'as Person' shouldn't be necessary to please the IDE.
If not, then it depends on how good the IDE is to infer the types (if possible). IntelliJ does a pretty good job here, given it can infer the type from where the collection is created. I guess it's because it knows details about most of the Groovy collection API.

As an example, IntelliJ infers that 'it' and 'found' must be a Date in the following example:

                def list = [new Date()-1, new Date()+1]
                def found = list.find {it > new Date()}
                println found.time


But when it comes to passing around closures, it gets really complicated, as Cédric's link explains.

Ronny



On Jul 20, 2012, at 3:07 PM, Wujek Srujek wrote:

> I have the following code:
>
> def found = getAll().find { Person it -> it.login == login } as Person
>
> In order for found to be inferred as Person, I must declare it as Person and cast the result to Person, otherwise type checking tells me that it is object. The problems seems to be that all such methods in DGM work with objects and return objects. Can this be somehow changed, or is it too late, impossible, unnecessary?
>
> wujek


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Ronny Løvtangen | 20 Jul 2012 15:54

Re: [groovy-user] make find, each and so on type-aware?

Ahh, sorry, I missed the <at> TypeChecked context :)

Ronny


On Jul 20, 2012, at 3:49 PM, Wujek Srujek wrote:

It returns Iterable<Person>. And I am using IntelliJ ;d It is smart most of the time, but that's not the problem here - I am using it with <at> TypeChecked and it results in an error (for example: 'Object has no attribute 'login'' or similar, or later, when I try to access found). True, if I don't use <at> TypeChecked, I get no warnings, but that's not the point here ;d

wujek

On Fri, Jul 20, 2012 at 3:41 PM, Ronny Løvtangen <lists-Zb5NtxTwiyqaMJb+Lgu22Q@public.gmane.org> wrote:
Does your getAll() return a Collection or Collection<Person>?
If the collection uses generics then 'as Person' shouldn't be necessary to please the IDE.
If not, then it depends on how good the IDE is to infer the types (if possible). IntelliJ does a pretty good job here, given it can infer the type from where the collection is created. I guess it's because it knows details about most of the Groovy collection API.

As an example, IntelliJ infers that 'it' and 'found' must be a Date in the following example:

                def list = [new Date()-1, new Date()+1]
                def found = list.find {it > new Date()}
                println found.time


But when it comes to passing around closures, it gets really complicated, as Cédric's link explains.

Ronny



On Jul 20, 2012, at 3:07 PM, Wujek Srujek wrote:

> I have the following code:
>
> def found = getAll().find { Person it -> it.login == login } as Person
>
> In order for found to be inferred as Person, I must declare it as Person and cast the result to Person, otherwise type checking tells me that it is object. The problems seems to be that all such methods in DGM work with objects and return objects. Can this be somehow changed, or is it too late, impossible, unnecessary?
>
> wujek


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email




Wujek Srujek | 20 Jul 2012 16:08
Picon

Re: [groovy-user] make find, each and so on type-aware?

No, my bad, I had not mentioned it before.



On Fri, Jul 20, 2012 at 3:54 PM, Ronny Løvtangen <lists <at> lovtangen.com> wrote:
Ahh, sorry, I missed the <at> TypeChecked context :)

Ronny


On Jul 20, 2012, at 3:49 PM, Wujek Srujek wrote:

It returns Iterable<Person>. And I am using IntelliJ ;d It is smart most of the time, but that's not the problem here - I am using it with <at> TypeChecked and it results in an error (for example: 'Object has no attribute 'login'' or similar, or later, when I try to access found). True, if I don't use <at> TypeChecked, I get no warnings, but that's not the point here ;d

wujek

On Fri, Jul 20, 2012 at 3:41 PM, Ronny Løvtangen <lists-Zb5NtxTwiyqaMJb+Lgu22Q@public.gmane.org> wrote:
Does your getAll() return a Collection or Collection<Person>?
If the collection uses generics then 'as Person' shouldn't be necessary to please the IDE.
If not, then it depends on how good the IDE is to infer the types (if possible). IntelliJ does a pretty good job here, given it can infer the type from where the collection is created. I guess it's because it knows details about most of the Groovy collection API.

As an example, IntelliJ infers that 'it' and 'found' must be a Date in the following example:

                def list = [new Date()-1, new Date()+1]
                def found = list.find {it > new Date()}
                println found.time


But when it comes to passing around closures, it gets really complicated, as Cédric's link explains.

Ronny



On Jul 20, 2012, at 3:07 PM, Wujek Srujek wrote:

> I have the following code:
>
> def found = getAll().find { Person it -> it.login == login } as Person
>
> In order for found to be inferred as Person, I must declare it as Person and cast the result to Person, otherwise type checking tells me that it is object. The problems seems to be that all such methods in DGM work with objects and return objects. Can this be somehow changed, or is it too late, impossible, unnecessary?
>
> wujek


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email






Gmane