RosettaCodeData/Task/Introspection/D/introspection.d

33 lines
1.1 KiB
D
Raw Permalink Normal View History

2014-01-17 05:32:22 +00:00
// Some module-level variables (D doesn't have a global scope).
immutable x = 3, y = 100, z = 3_000;
short w = 1; // Not an int, must be ignored.
immutable s = "some string"; // Not an int, must be ignored.
2013-10-27 22:24:23 +00:00
void main() {
2014-01-17 05:32:22 +00:00
import std.compiler, std.math, std.traits;
2013-10-27 22:24:23 +00:00
// Compile-time constants of the compiler version:
2014-01-17 05:32:22 +00:00
static assert(version_major > 1 && version_minor > 50,
"I can't cope with this compiler version.");
2013-10-27 22:24:23 +00:00
immutable bloop = 10;
// To check if something compiles:
2014-01-17 05:32:22 +00:00
static if (__traits(compiles, bloop.abs)) {
2013-10-27 22:24:23 +00:00
pragma(msg, "The expression is compilable.");
auto x = bloop.abs;
} else {
pragma(msg, "The expression can't be compiled.");
}
2014-01-17 05:32:22 +00:00
import std.stdio;
immutable s = 10_000; // Not at module scope, must be ignored.
int tot = 0;
/*static*/ foreach (name; __traits(allMembers, mixin(__MODULE__)))
2015-02-20 00:35:01 -05:00
static if (name != "object" &&
is(int == Unqual!(typeof(mixin("." ~ name)))))
2014-01-17 05:32:22 +00:00
tot += mixin("." ~ name);
writeln("Total of the module-level ints (could overflow): ", tot);
2013-10-27 22:24:23 +00:00
}