Re: [groovy-user] make find, each and so on type-aware?
Ronny Løvtangen <
lists@...>
2012-07-20 13:41:20 GMT
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