digitalmars.D.learn - need `this` for `this` of type `ref safe Test(string reg_arg)
- rempas (21/21) Jun 18 2023 Ok, so I'm having a struct that has a constructor that takes a
- Paul Backus (10/31) Jun 18 2023 `__ctor` doesn't create a new object, it initializes an existing
- rempas (6/15) Jun 18 2023 Thank you! Do you know any other way to do it without using
- Paul Backus (18/32) Jun 18 2023 No, there is no way to pass template arguments to a constructor
- rempas (3/20) Jun 18 2023 Thank you! This will make me change the design of my API a little
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)`
Jun 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: ```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"); } ```
Jun 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: ```d 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.
Jun 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: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"); } ````__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".
Jun 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: ```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"); } ```Thank you! This will make me change the design of my API a little bit but it will do ;)
Jun 18 2023