Thread overview
need `this` for `this` of type `ref @safe Test(string reg_arg)
Jun 18, 2023
rempas
Jun 18, 2023
Paul Backus
Jun 18, 2023
rempas
Jun 18, 2023
Paul Backus
Jun 18, 2023
rempas
June 18, 2023

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:

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 thisforthisof typeref @safe Test(string reg_arg)`

June 18, 2023

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:

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 thisforthisof typeref @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:

void main() {
  Test test;
  test.__ctor!("non_def")("Hello");
}
June 18, 2023

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:

void main() {
  Test test;
  test.__ctor!("non_def")("Hello");
}

Thank you! Do you know any other way to do it without using "__ctor".

I'm not sure if it was clear that it's not that I specifically want to do it with "__ctor" so I'm making it clear just to be sure.

June 18, 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:

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:

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");
}
June 18, 2023

On Sunday, 18 June 2023 at 19:22:45 UTC, Paul Backus wrote:

>

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:

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");
}

Thank you! This will make me change the design of my API a little bit but it will do ;)