What's the use of 'typeof','typeid','__traits','is' ?
Paul Backus
snarwin at gmail.com
Sun Aug 24 03:29:30 UTC 2025
On Sunday, 24 August 2025 at 03:13:38 UTC, David T. Oxygen wrote:
> Hello every kind and helpful D programmers, I need your help.
>
> I've met two problems about getting types.
> In D, we can use many keywords to get the type of an object,
> such as `typeof`,`typeid`,`is`.
> However, I don't know the differences between them well. I also
> don't know how to use them in D...
`typeof` is evaluated at compile time and gives the type of an
expression or symbol as seen by the compiler.
`typeid` is evaluated at runtime and gives the [`TypeInfo`][1]
object containing information about an object's runtime type.
[1]: https://dlang.org/library/object/type_info.html
The difference between `typeof` and `typeid` can be seen in the
following program:
class Example {}
void main()
{
auto object = new Example;
check(object);
}
void check(Object o)
{
import std.stdio;
writeln(typeof(o).stringof); // Object
writeln(typeid(o).toString); // Example
}
More information about the Digitalmars-d-learn
mailing list