September 23, 2020
On Wednesday, 23 September 2020 at 20:19:04 UTC, data pulverizer wrote:
> This has prompted me to write a data structure that I thought would be impossible until now. [...SNIP...]

Here is the function with the correct template constraint:

```
auto makeChain(Args...)(Args args)
if(Args.length > 2 && is(Args[0] == Start) && is(Args[Args.length - 1] == End))
{
  args[0].next = args[1];
  static foreach(i; 1..(Args.length - 1))
  {
    args[i].prev = args[i - 1];
    args[i].next = args[i + 1];
  }
  args[$ - 1].prev = args[$ - 2];
  return args[0];
}
```

September 23, 2020
On Wednesday, 23 September 2020 at 20:19:04 UTC, data pulverizer wrote:
> This has prompted me to write a data structure that I thought would be impossible until now....

False alarm:

```
writeln("typeof(x.next): ", typeof(x.next).stringof);
```
gives:

```
typeof(x.next): const(Node)
```

Oh well.
September 24, 2020
On Wednesday, 23 September 2020 at 18:37:45 UTC, wjoe wrote:
> I have some similar functions:
>
> void register(C: IFoo)()
> {
>   _insert!C();
> }
>
> void register(C)() if (behavesLikeFoo!C)
> {
>   _insert!C();
> }
>
> There are more overloads with parameters so I want to merge them
>
> void register(C, ARGS...)(ARGS args) if (behavesLikeFoo!C || isInstanceOf!(C, IFoo))
> {
>   _insert!C(args);
> }
>
> I found a lot of information on how to do this at runtime but not at compile time.
> std.traits: isInstanceOf doesn't work. Neither did anything I tried with typeid etc.
>
> The behavesLikeFoo constraint works as expected but it accepts any class no matter whether or not it implements the interface.

see if "import std.traits: isImplicitlyConvertible" helps you.

September 24, 2020
On Wednesday, 23 September 2020 at 20:19:04 UTC, data pulverizer wrote:
Thinking more about it this ...

> class Middle: Node
> {
>   Node prev;
>   Node next;
> }
won't work because the I've told the compiler that the static type is "Node", my original design was something like this:

```
struct Middle(P, N)
{
  P prev;
  N next;
}
```

which also won't work because it can't actually be instantiated (recursive), and the start and end nodes must be different types (and you can't substitute the middle node for the start and end node). It's the simplest case because there could be more than one type of middle node. More generally, correct me if I'm wrong, but I don't think these kinds of statically multityped tree structures can be formed in static languages so I've been using tuples - at least for the simple case I outlined.

1 2
Next ›   Last »