groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <blackd...@gmx.org>
Subject Re: extending a class with an @Delegate
Date Thu, 23 Mar 2017 12:53:55 GMT


On 23.03.2017 11:56, paul wrote:
> Suppose I have two Classes, one extending the other:
>
> class Person{ String name, int age}
> class Pupil extends Person {int grade}
>
> Now to manage several Persons in a List, and add some custom methods, I
> use the @Delegate like
>
> class Personlist{
>   @Delegate List<Person> persons
>
>   Personlist underAge(n){persons.findAll{it.age<=n} as Personlist}
> }
>
> Pupillist should have the same methods (since each pupil is a person):
>
> class Pupillist extends Personlist{
>   Pupillist inGrade(g){persons.findAll{it.grade==g} as Pupillist}
> }
>
> Now I want to chain several of these methods like this:
> def pl = [...] as Pupillist
> pl.underAge(12).inGrade(9)
>
> throws a MissingMethodException, because `pl.underAge` gets cast into a
> Personlist, which has no method `inGrade`.


How about not working with Delegate and instead use a category for the 
methods and just return a list?

bye Jochen

Mime
View raw message