How to construct a struct that does not explicitly define a constructor

Meta jared771 at gmail.com
Fri Feb 28 23:31:23 UTC 2025


```d
struct Test
{
     int n;
     float f;

     static Test opCall(int n, float f)
     {
         return Test(n, f);
     }
}

void main()
{
     Test(1, 2.0);
}
```

This code causes an infinite loop because the `Test(n, f)` inside 
the static `opCall` is interpreted as a recursive call to that 
same `opCall` function - not a call to the struct's constructor 
(or however this is interpreted by the compiler when there is no 
explicit struct/union constructor defined).

I tried doing `return Test.__ctor(n, f)` but it says that symbol 
doesn't exist. Is there any way to explicitly call the struct's 
constructor so it doesn't cause an infinitely recursive call to 
`opCall`?



More information about the Digitalmars-d-learn mailing list