Thread overview
Find the heir.
Mar 29, 2020
TodNaz
Mar 29, 2020
Stefan Koch
Mar 29, 2020
Simen Kjærås
Mar 30, 2020
WebFreak001
March 29, 2020
Hello!
>class A
>{
>    ...
>}
>
>class B : A
>{
>    ...
>}
>
>class C : A
>{
>    ...
>}
>
>A example1;
>B example2 = new B(...);
>A = example2;
>auto heir = A.whoheir(); ///

The question in this code is: is it possible to track the class inheritor? Or is it beyond D?
Sorry if the question is fool ...

March 29, 2020
On Sunday, 29 March 2020 at 14:04:53 UTC, TodNaz wrote:
> Hello!
>>class A
>>{
>>    ...
>>}
>>
>>class B : A
>>{
>>    ...
>>}
>>
>>class C : A
>>{
>>    ...
>>}
>>
>>A example1;
>>B example2 = new B(...);
>>A = example2;
>>auto heir = A.whoheir(); ///
>
> The question in this code is: is it possible to track the class inheritor? Or is it beyond D?
> Sorry if the question is fool ...

It is not generally known who has inherited a class from the parents perspective.

March 29, 2020
On Sunday, 29 March 2020 at 14:04:53 UTC, TodNaz wrote:
> Hello!
>>class A
>>{
>>    ...
>>}
>>
>>class B : A
>>{
>>    ...
>>}
>>
>>class C : A
>>{
>>    ...
>>}
>>
>>A example1;
>>B example2 = new B(...);
>>A = example2;
>>auto heir = A.whoheir(); ///
>
> The question in this code is: is it possible to track the class inheritor? Or is it beyond D?
> Sorry if the question is fool ...

The question is a bit unclear - what is whoheir expected to return? This is one way that may or may not fulfill your requirements:

module foo;
class A {
    string whoheir() {
        return typeid(this).name;
    }
}
class B : A {}
class C : A {}

unittest {
    A a = new B();
    assert(a.whoheir == "foo.B");
    a = new C();
    assert(a.whoheir == "foo.C");
}

--
  Simen
March 30, 2020
On Sunday, 29 March 2020 at 15:07:37 UTC, Simen Kjærås wrote:
> On Sunday, 29 March 2020 at 14:04:53 UTC, TodNaz wrote:
>> Hello!
>>>class A
>>>{
>>>    ...
>>>}
>>>
>>>class B : A
>>>{
>>>    ...
>>>}
>>>
>>>class C : A
>>>{
>>>    ...
>>>}
>>>
>>>A example1;
>>>B example2 = new B(...);
>>>A = example2;
>>>auto heir = A.whoheir(); ///
>>
>> The question in this code is: is it possible to track the class inheritor? Or is it beyond D?
>> Sorry if the question is fool ...
>
> The question is a bit unclear - what is whoheir expected to return? This is one way that may or may not fulfill your requirements:
>
> module foo;
> class A {
>     string whoheir() {
>         return typeid(this).name;
>     }
> }
> class B : A {}
> class C : A {}
>
> unittest {
>     A a = new B();
>     assert(a.whoheir == "foo.B");
>     a = new C();
>     assert(a.whoheir == "foo.C");
> }
>
> --
>   Simen

to extend on this: typeid(...) returns a TypeInfo so you can perform a bit of runtime reflection, see https://dlang.org/phobos/object.html#.TypeInfo

To check for exact matches if your class is B or C, you can also use
typeid(variable) == typeid(B)
if you don't want to compare arbitrary strings. (better code style)

There are also libraries to help you do this sort of runtime reflection like witchcraft: https://code.dlang.org/packages/witchcraft

or a more current hunt fork, though I don't know yet what it changes: https://github.com/huntlabs/hunt-reflection