Type sniffing at runtime

Alex sascha.orlov at gmail.com
Sat May 16 06:12:34 UTC 2020


On Saturday, 16 May 2020 at 05:22:49 UTC, n0den1te wrote:
> [...]

For example, like this:

´´´
import std;

alias types = AliasSeq!(
     bool, byte, ubyte, short, ushort, int, uint, long, ulong, 
float, double, real, char, wchar, dchar
);

void main()
{
	static foreach(type; types)
     {
         sniffType!type;
     }
}


void sniffType(T)() {
     writeln("Type           : ", T.stringof);
     writeln("Length in bytes: ", T.sizeof);
     static if(__traits(compiles, T.min))
         writeln("Minimum value  : ", T.min);
     writeln("Maximum value  : ", T.max);
     writeln("Initial value  : ", T.init);
}

´´´

However, for the char, wchar and dchar types the results of min, 
max and init are somewhat cumbersome. And T.min does not compile 
for the floating point types.




More information about the Digitalmars-d-learn mailing list