The Final(ize) Challenge

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon May 18 14:06:56 PDT 2009


Denis Koroskin wrote:
> On Mon, 18 May 2009 20:12:40 +0400, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> wrote:
> 
>> Ok, now with the advent (or rediscovery) of allMembers which I had no 
>> idea existed, we're ready to start some serious butt-kick reflection 
>> facilities.
>>
>> For starters, I'd like to present you with the following challenge. 
>> Given any class C, e.g.:
>>
>> class C
>> {
>>      void foo(int) { ... }
>>      int bar(string) { ... }
>> }
>>
>> define a template class Finalize(T) such that Finalize!(C) is the same 
>> as the following hand-written class:
>>
>> final class FinalizeC : C
>> {
>>      final void foo(int a) { return super.foo(a); }
>>      final int bar(string a) { return super.bar(a); }
>> }
>>
>> Finalize is cool when you need some functionality from an exact class 
>> and you don't want to pay indirect calls throughout. All calls through 
>> Finalize!(C)'s methods will resolve to static calls.
>>
>>
>> Have at it!
>>
>> Andrei
> 
> template Finalize(T)
> {
>    final class Finalize : T
>    {
>        this(Args...)(Args args)
>        {
>            super(args);
>        }
>    }
> }
> 
> The following code /should/ work as you ask. There is no need to iterate 
> over all the methods and make a final version out of it for two reasons
> 1) It is redundant
> 2) It creates an additional overhead
> 
> unless that's was you *really* need.
> 
> Future work: Finalize!(T) should resolve to T iff T is already final.

Good point! Now define Unfinalize that opens the final methods of a 
class for method overriding.

class A
{
     final int foo(string) { ... }
}

class UnfinalizeA : A
{
     int foo(string a) { return super.foo(a); }
}


Andrei



More information about the Digitalmars-d mailing list