Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31993 > unrolled thread

anonymous generic class with reflexive type argument...

Started byAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
First post2015-07-12 18:44 +0000
Last post2015-07-17 11:40 +0000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  anonymous generic class with reflexive type argument... Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2015-07-12 18:44 +0000
    Re: anonymous generic class with reflexive type argument... Lew <lewbloch@gmail.com> - 2015-07-16 17:16 -0700
      Re: anonymous generic class with reflexive type argument... Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2015-07-17 11:40 +0000

#31993 — anonymous generic class with reflexive type argument...

FromAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
Date2015-07-12 18:44 +0000
Subjectanonymous generic class with reflexive type argument...
Message-ID<slrnmq5deh.uoi.avl@login.logic.tuwien.ac.at>
A while ago, there was a thread about a trick (or pattern) involving 
a reflexive type argument:

  abstract class Base<T extends Base<T>> {
     T doAction() {
        // do something ...
        return self(); // for chain-calling.
     }
     abstract T self();
  }

  class Sub extends Base<Sub> {
     Sub self() { return this; }
  }

So far, that's the pattern (just for the context).

Now my question is: does there exist also a trick to create an 
anonymous subclass directly of Base, and if so, how would that
look like?

  ... = new Base<???>() {
     ??? doAction() {
        // do something more specific
        return self();
     }
     ??? self() { return this; }
  }

Of course I could create some non-anonymous and non-generic helper
class Foo extends Base<Foo>, implement self() returning Foo, and then
create an anonymous Subclass of Foo, but is there also a direct way?

[toc] | [next] | [standalone]


#32052

FromLew <lewbloch@gmail.com>
Date2015-07-16 17:16 -0700
Message-ID<ca4e353c-8152-4426-8d8d-c06a396ba37f@googlegroups.com>
In reply to#31993
On Sunday, July 12, 2015 at 11:45:53 AM UTC-7, Andreas Leitgeb wrote:
> A while ago, there was a thread about a trick (or pattern) involving 
> a reflexive type argument:
> 
>   abstract class Base<T extends Base<T>> {
>      T doAction() {
>         // do something ...
>         return self(); // for chain-calling.
>      }
>      abstract T self();
>   }
> 
>   class Sub extends Base<Sub> {
>      Sub self() { return this; }
>   }
> 
> So far, that's the pattern (just for the context).
> 
> Now my question is: does there exist also a trick to create an 
> anonymous subclass directly of Base, and if so, how would that
> look like?
> 
>   ... = new Base<???>() {
>      ??? doAction() {
>         // do something more specific
>         return self();
>      }
>      ??? self() { return this; }
>   }
> 
> Of course I could create some non-anonymous and non-generic helper
> class Foo extends Base<Foo>, implement self() returning Foo, and then
> create an anonymous Subclass of Foo, but is there also a direct way?

You'll have to return the immediate supertype, which is the usual practice with anonymous subtypes. 

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#32059

FromAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
Date2015-07-17 11:40 +0000
Message-ID<slrnmqhqgh.uoi.avl@login.logic.tuwien.ac.at>
In reply to#32052
Lew <lewbloch@gmail.com> wrote:
> On Sunday, July 12, 2015 at 11:45:53 AM UTC-7, Andreas Leitgeb wrote:
>> A while ago, there was a thread about a trick (or pattern) involving 
>> a reflexive type argument:
>>   abstract class Base<T extends Base<T>> {
>>      T doAction() {
>>         // do something ...
>>         return self(); // for chain-calling.
>>      }
>>      abstract T self();
>>   }
>>   class Sub extends Base<Sub> {
>>      Sub self() { return this; }
>>   }
>> So far, that's the pattern (just for the context).
>> 
>> Now my question is: does there exist also a trick to create an 
>> anonymous subclass directly of Base, and if so, how would that
>> look like?
>> 
>>   ... = new Base<???>() {
>>      ??? doAction() {
>>         // do something more specific
>>         return self();
>>      }
>>      ??? self() { return this; }
>>   }
>> Of course I could create some non-anonymous and non-generic helper
>> class Foo extends Base<Foo>, implement self() returning Foo, and then
>> create an anonymous Subclass of Foo, but is there also a direct way?
>
> You'll have to return the immediate supertype, which is the usual
> practice with anonymous subtypes. 

I don't see how to apply this usual practice to the the particular
given generic class Base, short of using rawtypes and @SuppressWarnings.

Thanks, anyway.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web