On 6/23/23 10:31 AM, FeepingCreature wrote:
On Friday, 23 June 2023 at 14:22:24 UTC, DLearner wrote:
Hi

Was looking for compile-time detection of a struct variable.
However, the following test code gave the two 'FAILS' shown below.
Comments?
```
void main() {
   import std.stdio : writeln;
   import std.traits;

   string mxnTst(string VarName) {
      return

      `static if (is(typeof(` ~ VarName ~ `) == char)) {` ~
         `writeln("` ~ VarName ~ ` ", " is a char");` ~
      `} else static if (__traits(isPOD, typeof(` ~ VarName ~ `))) {` ~
         `writeln("` ~ VarName ~ ` ", " is a struct");` ~
      `} else static if (is(typeof(` ~ VarName ~ `) == int)) {` ~
         `writeln("` ~ VarName ~ ` ", " is an int");` ~
      `} else {` ~
         `static assert(false, "mxnTst Variable '` ~ VarName  ~ `' is of unknown type");` ~
      `}`
       ;
   }

   char char1;
   int  int1;
   byte byte1;

   struct foo {
      int  fooint;
      char foochar;
   }
   foo foovar1;

   mixin(mxnTst("char1"));   // Expected: char1 is a char. Actual: char1 is a char. (ok)    mixin(mxnTst("int1"));    // Expected: int1 is an int. Actual: int1 is a struct. (FAIL)    mixin(mxnTst("foovar1")); // Expected: foovar1 is a struct. Actual: foovar1 is a struct. (ok)    mixin(mxnTst("byte1"));   // Expected: Run to fail with the static assert message. Actual: byte1 is a struct. (FAIL)
}
```

```
static assert(__traits(isPOD, int)); // ok.
static assert(__traits(isPOD, byte)); // ok.
```
It's a bug in either the spec or the compiler.

It should be a spec change. Change POD to say "type" instead of "struct".

The goal of `isPOD` is to determine how careful generic code needs to be to pass the type around, or copy it. Changing it to false implies that it is not "plain old data". I.e. it has a destructor, it has hidden members, or it cannot be copied via bit copying (all of these do not fit the type in question).

The only other option is to error on calling `__traits(isPOD, char)`, but I think that's even worse.

-Steve

Reply via email to