On Sunday, 18 June 2023 at 17:43:01 UTC, rempas wrote:
Ok, so I'm having a struct that has a constructor that takes a
template parameter. I suppose this question could also be named
`how to initialize constructors with template parameters` but
whatever! The funny thing is, I think that I may have already
found how to do it in the past but I forgot... This time, I'm
going to add the code in a big module in my project so I'm not
going to have it as a reference and I won't forget it.
Here is a small sample code:
```d
import std.stdio;
struct Test {
this(string type = "def")(string reg_arg) {
writeln("reg_arg: ", reg_arg);
}
}
void main() {
auto test = Test.__ctor!("non_def")("Hello");
}
```
And the error I get: `test.d(10): Error: need `this` for `this`
of type `ref @safe Test(string reg_arg)`
`__ctor` doesn't create a new object, it initializes an existing
object. You need to create the object first, then call `__ctor`
on it:
```d
void main() {
Test test;
test.__ctor!("non_def")("Hello");
}
```