Automatic method overriding in sub-classes

Tofu Ninja via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 26 17:12:40 PDT 2015


On Tuesday, 27 October 2015 at 00:07:36 UTC, Ali Çehreli wrote:
> On 10/26/2015 04:25 PM, Tofu Ninja wrote:
>> ##################################
>> class A
>> {
>>       void foo(this T)() { writeln(T.stringof); }
>>       void bar(auto override this T)() { writeln(T.stringof); }
>> }
>>
>> class B : A {}
>>
>> void main()
>> {
>>       A a = new A();
>>       a.foo(); // prints "A"
>>       a.bar(); // prints "A"
>>
>>       B b = new B();
>>       b.foo(); // prints "B"
>>       b.bar(); // prints "B"
>>
>>       A c = new B();
>>       c.foo(); // prints "A"
>>       c.bar(); // prints "B" <-- main advantage, method is 
>> virtual
>> }
>> ##################################
>
> I don't understand all uses of the request but typeid() returns 
> a TypeInfo reference, which is about the actual type of an 
> object. The following change produces the expected output in 
> this case (except, it has the added module name):
>
> void bar() { writeln(typeid(this).name); }
>
> The output:
>
> A
> deneme.A
> B
> deneme.B
> A
> deneme.B
>
> Ali

For something as simple as the name, yea, type id works. But for 
something like iterating over UDA's or looking at 
__traits(allMembers) or something like that, type id is not what 
is needed.


More information about the Digitalmars-d mailing list