groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paul <pl.grue...@gmail.com>
Subject extending a class with an @Delegate
Date Thu, 23 Mar 2017 10:56:12 GMT
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`.

I know I could work around this by
a) in the chain of methods, first call the more specialized methods, 
then the more general ones – but I don't want to care about that every time.
b) override the `underAge` function in Pupillist with a call to `super` 
– but then I'd have to write that boilerplate code I'd hoped to leave 
behind by using @Delegate.

Can someone tell me how to properly solve this (design?-) issue?

Thanks in advance
Paul
-- 
typed with Neo 2 -- an ergonomically optimized keyboard layout
for German, English, programming, and science

http://neo-layout.org/

Mime
View raw message