Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
Thank you all for the info! I'll try to find another way to do it as it is not possible to match the exact behavior I want! Have a great day everyone!

Re: How do I initialize a templated constructor?

2022-08-08 Thread Ali Çehreli via Digitalmars-d-learn
Here is another one that uses nested templates: import std.stdio; template TestArray(ulong element_n) { struct TestArrayImpl(Type) { int[element_n] elements; this(ulong number) { pragma(msg, "The type is: ", Type); writeln("Constructing with ", number); } } auto

Re: How do I initialize a templated constructor?

2022-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/8/22 9:36 AM, Steven Schveighoffer wrote: On 8/8/22 1:38 AM, rempas wrote: In the following struct (as an example, not real code): ``` struct TestArray(ulong element_n) {    int[element_n] elements;    this(string type)(ulong number) { pragma(msg, "The type is: " ~

Re: How do I initialize a templated constructor?

2022-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/8/22 1:38 AM, rempas wrote: In the following struct (as an example, not real code): ``` struct TestArray(ulong element_n) {   int[element_n] elements;   this(string type)(ulong number) {     pragma(msg, "The type is: " ~ typeof(type).stringof);   } } ``` I want to create it and be

Re: How do I initialize a templated constructor?

2022-08-08 Thread Ali Çehreli via Digitalmars-d-learn
On 8/7/22 22:38, rempas wrote: > I want to create it and be able to successfully initialize the template > parameters > of the constructor but until now, I wasn't able to find a way to > successfully do > that. The following method uses a convenience function but it's not really needed: import

Re: How do I initialize a templated constructor?

2022-08-08 Thread WebFreak001 via Digitalmars-d-learn
On Monday, 8 August 2022 at 05:38:31 UTC, rempas wrote: In the following struct (as an example, not real code): ``` struct TestArray(ulong element_n) { int[element_n] elements; this(string type)(ulong number) { pragma(msg, "The type is: " ~ typeof(type).stringof); } } ``` I want to

Re: How do I initialize a templated constructor?

2022-08-08 Thread zjh via Digitalmars-d-learn
On Monday, 8 August 2022 at 12:26:50 UTC, rempas wrote: On Monday, 8 August 2022 at 11:03:21 UTC, Dom Disc wrote: You should first describe what you want to do clearly.

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
On Monday, 8 August 2022 at 11:03:21 UTC, Dom Disc wrote: But if you only want to know the type of the parameter, you can do this: ```D struct TestArray(ulong element_n) { int[element_n] elements; this(type)(type number) { pragma(msg, "The type is: " ~ type.stringof); } } ```

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
On Monday, 8 August 2022 at 08:27:49 UTC, bauss wrote: Yeah I think the only template argument you can have for constructors are `this` which will refer to things like the class that inherited the current class etc. not sure what else, but you can't really pass anything to it yourself

Re: How do I initialize a templated constructor?

2022-08-08 Thread Dom Disc via Digitalmars-d-learn
And then you can instantiate it with ```D auto val = TestArray!10(ubyte(60)); // if you want type to be ubyte ```

Re: How do I initialize a templated constructor?

2022-08-08 Thread Dom Disc via Digitalmars-d-learn
On Monday, 8 August 2022 at 06:58:42 UTC, bauss wrote: On Monday, 8 August 2022 at 05:38:31 UTC, rempas wrote: In the following struct (as an example, not real code): ``` struct TestArray(ulong element_n) { int[element_n] elements; this(string type)(ulong number) { pragma(msg, "The

Re: How do I initialize a templated constructor?

2022-08-08 Thread bauss via Digitalmars-d-learn
On Monday, 8 August 2022 at 07:37:16 UTC, rempas wrote: Thank you for all the great info! Unfortunately, while there is no problem in this example, this will not do for my real code as I need to have the argument in the constructor. Alternative, I have to change the design of the program

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
On Monday, 8 August 2022 at 06:58:42 UTC, bauss wrote: ``` this(string type)(ulong number) { ``` You cannot do this. Instead your type should look like this: First let's change it up a little bit. ``` struct TestArray(ulong element_n, string type) { int[element_n] elements; this(ulong

Re: How do I initialize a templated constructor?

2022-08-08 Thread bauss via Digitalmars-d-learn
On Monday, 8 August 2022 at 05:38:31 UTC, rempas wrote: In the following struct (as an example, not real code): ``` struct TestArray(ulong element_n) { int[element_n] elements; this(string type)(ulong number) { pragma(msg, "The type is: " ~ typeof(type).stringof); } } ``` I want to

How do I initialize a templated constructor?

2022-08-07 Thread rempas via Digitalmars-d-learn
In the following struct (as an example, not real code): ``` struct TestArray(ulong element_n) { int[element_n] elements; this(string type)(ulong number) { pragma(msg, "The type is: " ~ typeof(type).stringof); } } ``` I want to create it and be able to successfully initialize the