Checking for manifest constants
Bogdan
szabobogdan at yahoo.com
Fri Mar 5 08:23:09 UTC 2021
I was using a trick with dmd to check for manifest constants
which worked until dmd v2.094. Yesterday I tried it on the latest
compiler and it failed with:
source/introspection/manifestConstant.d(37,28): Error: need this
for name of type string
source/introspection/type.d(156,13): Error: value of this is not
known at compile time
any ideas how to fix it? or, is it a bug with dmd?
```
/// Check if a member is manifest constant
bool isManifestConstant(T, string name)() {
mixin(`return is(typeof(T.init.` ~ name ~ `)) &&
!is(typeof(&T.init.` ~ name ~ `));`);
}
/// ditto
bool isManifestConstant(alias T)() {
return is(typeof(T)) && !is(typeof(&T));
}
enum globalConfig = 32;
int globalValue = 22;
unittest {
struct Test {
enum config = 3;
int value = 2;
}
static assert(isManifestConstant!(Test.config));
static assert(isManifestConstant!(Test, "config"));
static assert(isManifestConstant!(globalConfig));
static assert(!isManifestConstant!(Test.value));
static assert(!isManifestConstant!(Test, "value"));
static assert(!isManifestConstant!(globalValue));
}
void main() {}
```
More information about the Digitalmars-d-learn
mailing list