need `this` for `this` of type `ref @safe Test(string reg_arg)
Paul Backus
snarwin at gmail.com
Sun Jun 18 19:22:45 UTC 2023
On Sunday, 18 June 2023 at 19:05:19 UTC, rempas wrote:
> On Sunday, 18 June 2023 at 18:17:16 UTC, Paul Backus wrote:
>>
>> `__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");
>> }
>> ```
>
> Thank you! Do you know any other way to do it without using
> "__ctor".
No, there is no way to pass template arguments to a constructor
without using `__ctor`.
My recommendation is to use a free function or a `static` method
instead. For example:
```d
import std.stdio;
struct Test {}
Test makeTest(string type = "def")(string reg_arg)
{
writeln("reg_arg: ", reg_arg);
return Test();
}
void main()
{
auto test = makeTest!"non_def"("Hello");
}
```
More information about the Digitalmars-d-learn
mailing list