Thread overview
TypeInfo_Class confusion
5 days ago
Brother Bill
4 days ago
user1234
4 days ago
user1234
4 days ago
Brother Bill
5 days ago

On page 344 of "Programming in D", we have this snippet:

 TypeInfo_Class info = a.classinfo;
 string runtimeTypeName = info.name;

I've expanded it to source/app.d which when runs produces output:

app.Violin
app.Guitar

app.Violin
TypeInfo_Class
object.TypeInfo

I was expecting something more concrete than TypeInfo_Class or object.TypeInfo, such as "Violin" or "StringInstrument".

Am I missing something or have I made a mistake?

source/app.d

import std.stdio;

void main() {
    TypeInfo v = typeid(foo(1));
    TypeInfo g = typeid(foo(2));

    assert(v != g); // ← the two types are not the same

    writeln(v);
    writeln(g);
    writeln;

    TypeInfo_Class v2 = typeid(foo(1));
    string className = v2.classinfo.name;
    string baseName = v2.classinfo.base.name;

    writeln(v2);
    writeln(className);
    writeln(baseName);
}

MusicalInstrument foo(int i) {
    return (i % 2) ? new Violin() : new Guitar();
}

class MusicalInstrument {
}

class StringInstrument : MusicalInstrument {
}

class Violin : StringInstrument {
}

class Guitar : StringInstrument {
}

5 days ago
The code looks fine.

TypeInfo is used at runtime to describe compile time constructs.

Classes require it for down casting, it is acquired from the runtime value.

For instance if the reference is null, the TypeInfo should be null as well.
4 days ago

On Saturday, 9 August 2025 at 11:13:42 UTC, Brother Bill wrote:

>

On page 344 of "Programming in D", we have this snippet:
[...]
I was expecting something more concrete than TypeInfo_Class or object.TypeInfo, such as "Violin" or "StringInstrument".

Am I missing something or have I made a mistake?

source/app.d

import std.stdio;

void main() {
    TypeInfo v = typeid(foo(1));
    TypeInfo g = typeid(foo(2));

    assert(v != g); // ← the two types are not the same

    writeln(v);
    writeln(g);
    writeln;

    TypeInfo_Class v2 = typeid(foo(1));
    string className = v2.classinfo.name;
    string baseName = v2.classinfo.base.name;

Change the last lines to

string className = v2.name;
string baseName = v2.base.name;

otherwise you operates on the TypeInfo_Class of the Type_Info class itself.

4 days ago

On Sunday, 10 August 2025 at 09:58:50 UTC, user1234 wrote:

>

On Saturday, 9 August 2025 at 11:13:42 UTC, Brother Bill wrote:

>

On page 344 of "Programming in D", we have this snippet:
[...]
I was expecting something more concrete than TypeInfo_Class or object.TypeInfo, such as "Violin" or "StringInstrument".

Am I missing something or have I made a mistake?

source/app.d

import std.stdio;

void main() {
    TypeInfo v = typeid(foo(1));
    TypeInfo g = typeid(foo(2));

    assert(v != g); // ← the two types are not the same

    writeln(v);
    writeln(g);
    writeln;

    TypeInfo_Class v2 = typeid(foo(1));
    string className = v2.classinfo.name;
    string baseName = v2.classinfo.base.name;

Change the last lines to

string className = v2.name;
string baseName = v2.base.name;

otherwise you operates on the TypeInfo_Class of the Type_Info class itself.

forgot to say... the confusion comes from the fact that typeid(a).classinfo is actually the same as typeid(typeid(a)). IIRC it was planned to remove .classinfo because of that lack of orthogonality.

4 days ago

On Sunday, 10 August 2025 at 10:07:13 UTC, user1234 wrote:

>

On Sunday, 10 August 2025 at 09:58:50 UTC, user1234 wrote:

>

On Saturday, 9 August 2025 at 11:13:42 UTC, Brother Bill wrote:

>

On page 344 of "Programming in D", we have this snippet:
[...]
I was expecting something more concrete than TypeInfo_Class or object.TypeInfo, such as "Violin" or "StringInstrument".

Am I missing something or have I made a mistake?

source/app.d

import std.stdio;

void main() {
    TypeInfo v = typeid(foo(1));
    TypeInfo g = typeid(foo(2));

    assert(v != g); // ← the two types are not the same

    writeln(v);
    writeln(g);
    writeln;

    TypeInfo_Class v2 = typeid(foo(1));
    string className = v2.classinfo.name;
    string baseName = v2.classinfo.base.name;

Change the last lines to

string className = v2.name;
string baseName = v2.base.name;

otherwise you operates on the TypeInfo_Class of the Type_Info class itself.

forgot to say... the confusion comes from the fact that typeid(a).classinfo is actually the same as typeid(typeid(a)). IIRC it was planned to remove .classinfo because of that lack of orthogonality.

Bingo! That's it. Thank you.