Unqualified class name

jfondren julian.fondren at gmail.com
Sun Aug 22 01:21:47 UTC 2021


On Sunday, 22 August 2021 at 00:18:18 UTC, Ali Çehreli wrote:
> I did not read the linked thread but a "this template 
> parameter" seems to work in this case:
>
> class Whoami {
>   string name(this This)() const {
>     return __traits(identifier, This);
>   }
> }
> class AnotherOne : Whoami { }
>
> unittest {
>   assert((new Whoami).name == "Whoami");
>   assert((new AnotherOne).name == "AnotherOne");
> }
>
> void main() {
> }
>
> Ali

That's not enough to make runtime dispatch out of static dispatch:

```d
class Whoami {
     string name(this This)() const {
         return __traits(identifier, This);
     }

     string rtname() const {
         import std.path : extension;

         return typeid(this).name.extension[1 .. $];
     }
}

class AnotherOne : Whoami { }

unittest {
     import std.algorithm : map, equal;

     auto list = [new Whoami, new AnotherOne];
     assert(list.map!(o => o.name).equal(["Whoami", "Whoami"]));
     assert(list.map!(o => o.rtname).equal(["Whoami", 
"AnotherOne"]));
}
```

The next stage of complication is to have compiled library code 
that loops over Whoami[] getting some subtypes of Whoami that 
were defined unknowable to the library.


More information about the Digitalmars-d-learn mailing list