February 28, 2023

Greetings to everyone who has fallen in love with D... 😀

I have a collaborative container (Node) and another convenience struct (S) where they work together. Node can be a struct or class, so a 3rd type is not allowed.

This is easy to do, static if(is... came to our rescue. But because the compiler interprets the alias first for some reason, we get this error message:

>

source_file.d(13): Error: no property CLL for type TestBP
source_file.d(55): Error: template instance source.S!(TestBP) error instantiating

I implemented the solution using the mixin template and dividing S into 2 parts. Moreover, I got rid of the static assert:

struct S(Node)
{
  static if(is(Node == struct))
  {
    private Node * data;
  }
  else static if(is(Node == class))
  {
    private Node data;
  }
  else static assert(0, "\nIt's not a container!");

  alias T = typeof(Node.CLL);

  this(T value) { data = new Node(value); }
}

struct S_part1(Node)
if(is(Node == struct) || is(Node == class))
{
  mixin S_part2!Node;
  this(T value) { data = new Node(value); }
}

template S_part2(Node)
{
  static if(is(Node == struct))
  {
    private Node * data;
  }
  else static if(is(Node == class))
  {
    private Node data;
  }

  alias T = typeof(Node.CLL);
}

class Bar {
  int data;
  alias CLL = data;
  this(int i) { data = i; }
}

struct Foo {
  int data;
  alias CLL = data;
  this(int i) { data = i; }
}

void main()
{
  // By-Pass Test:
  enum TestBP { None }
  S!TestBP test1;

  // Test okay:
  enum TestOK { CLL }
  S!TestOK test2;

  // New solution:
  S_part1!Foo foo;
  S_part1!Bar bar;
}

SDB@79