Unqualified class name

Jeremy T. Gibson jtgibson at telus.net
Sat Aug 21 21:19:50 UTC 2021


On Saturday, 21 August 2021 at 18:45:07 UTC, jfondren wrote:
> On Saturday, 21 August 2021 at 17:33:51 UTC, Jeremy T. Gibson 
> wrote:
>> is there a simple way to get the unqualified name of a class 
>> at runtime without having to pass it through std.format?  
>> `typeid(class).name` always yields the full classname, 
>> including its module information (i.e., 
>> "modulename.classname"), where I only want "classname" on its 
>> own.
>
> https://dlang.org/phobos/object.html#.TypeInfo_Class.name is 
> all you have there, and it's just a string, so the simple way 
> is to manipulate the string.

> string unqualified(string name) {
>     import std.string : lastIndexOf;
>     import std.algorithm : min;
>
>     const i = name.lastIndexOf('.');
>     return i == -1 ? name : name[min(i+1, $) .. $];

Parsing was always an option, but anytime I come across a 
solution where parsing/formatting seems to be the answer, it 
always feels more hacky than a genuine solution.  Hacks can be 
fine, especially in low-frequency code like the stuff I'm 
revisiting this month, but I always prefer direct library-based 
or language-based solutions whenever humanly possible.  Dlang's 
reference docs unfortunately assume a level of expertise with the 
language (especially, expertise with code generation and compiler 
specs) that makes it much harder for me to "ask the right 
questions" when I'm searching: I'm really not much of a 
programmer.  Heh.


More information about the Digitalmars-d-learn mailing list