Structs are Not Plain: A call for empty struct constructors

Nicholas Wilson iamthewilsonator at hotmail.com
Thu Sep 19 09:30:00 UTC 2019


On Thursday, 19 September 2019 at 09:02:39 UTC, FeepingCreature 
wrote:
> Let me lay out the problem first.
>
> We're doing unittesting with fixtures. A Fixture is a struct 
> that contains default values and mocks of the class structure 
> that is being tested. Unittests will have the form
>
> ```
> unittest {
>   with (Fixture()) {
>   }
> }
> ```

A workaround, wrap it with alias this in another struct:

```
import std.stdio;
struct Foo
{
     int a;
     this(int) { a = -1; }
     ~this() { writeln("~this(), a = ",a); }
}

struct Bar
{
     Foo f = Foo(1);
     alias f this;
}
void main()
{
     with(Bar())
     {
         writeln(a);
         a = 42;
     }
}
```


More information about the Digitalmars-d mailing list