August 08, 2022
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 std.stdio;

struct TestArray(ulong element_n, string type) {
  int[element_n] elements;
  mixin(type) member;
  pragma(msg, "The type is: ", typeof(member));

  this(ulong number) {
    writeln("Constructing with ", number);
  }
}

auto makeTestArray(ulong element_n, string type)(ulong number) {
  return TestArray!(element_n, type)(number);
}

void main() {
  auto ta = makeTestArray!(10, "int")(60);
}

Ali

August 08, 2022

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 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. Is there a way you guys know?  I have tried the following:

void main() {
   // Doesn't work
   auto val = TestArray!(10, "int")(60);

   // Doesn't work either
   auto val = TestArray!(10).TestArray!("int")(60);

   // Neither this works....
   auto val = TestArray!(10).this!("int")(60);
}

As with every question I make, the solution must be "betterC" compatible so I can use it.
Thanks a lot!

You cannot explicitly specify template parameters for constructors.

The only true solution is to use a factory function:

TestArray!T testarray(string s, T)(T val) {
   ... // code that depends on s here
   return TestArray!T(...) // call ctor here.
}

-Steve

August 08, 2022

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: " ~ typeof(type).stringof);
   }
}

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. Is there a way you guys know?  I have tried the following:

void main() {
   // Doesn't work
   auto val = TestArray!(10, "int")(60);

   // Doesn't work either
   auto val = TestArray!(10).TestArray!("int")(60);

   // Neither this works....
   auto val = TestArray!(10).this!("int")(60);
}

As with every question I make, the solution must be "betterC" compatible so I can use it.
Thanks a lot!

You cannot explicitly specify template parameters for constructors.

The only true solution is to use a factory function:

TestArray!T testarray(string s, T)(T val) {
    ... // code that depends on s here
    return TestArray!T(...) // call ctor here.
}

Just thought of another possibility:

struct StringAnnotated(string s, T)
{
   T val;
}

StringAnnotated!(s, T) annotate(string s, T)(T val)
{
   return StringAnnotated!(s, T)(val);
}

struct TestArray(ulong element_n)
{
   ...
   this(T)(T val) if (isInstanceOf!(StringAnnotated, T))
   {
      ...
   }
}

// use like
TestArray!10(60.annotate!"int");

-Steve

August 08, 2022
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 makeFor(string s)(ulong number) {
    return TestArrayImpl!(mixin(s))(number);
  }
}

void main() {
  auto ta = TestArray!10.makeFor!"int"(60);
}

Ali

August 08, 2022

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!

1 2
Next ›   Last »