TypeInfo_Class confusion

Brother Bill brotherbill at mail.com
Sat Aug 9 11:13:42 UTC 2025


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 {
}

```


More information about the Digitalmars-d-learn mailing list