Containers

Paul Backus snarwin at gmail.com
Wed Sep 1 00:26:33 UTC 2021


On Tuesday, 31 August 2021 at 23:56:51 UTC, Steven Schveighoffer 
wrote:
>> To put it bluntly, I have no idea how to achieve this. And I 
>> don't think we'll have a good set of collection before this is 
>> sorted out.
>
> I had an idea a long time ago, but ashamedly never brought it 
> to fruition. But long story short, I think we need a way to 
> specify "tail" for modifiers, and allow implicitly casting the 
> head. Then you'd have `tailconst(Vector!int)`, and you are 
> good. I actually had a decent syntax for it, but I'm not sure 
> it would fly anyway.

One problem I can forsee with this approach is that, from the 
compiler's point of view, there is not necessarily any 
predictable relationship between `qualifier(Template!T)` and 
`Template!(qualifier(T))`. For all it knows, you could have 
written code like the following:

```d
struct Vector(T)
{
     static if (is(T == const)) {
         // do one thing
     } else {
         // do something completely different
     }
}
```

Maybe in some restricted cases it could examine the 
uninstantiated template, determine that there's no funny business 
going on, and conclude that the implicit conversion is valid--but 
this kind of heuristic-based approach tends to be very brittle 
and compose poorly (exhibit A: [issue 1807][1]).

In general, if we want the compiler to know that a conversion 
from one template instantiation to another is valid, we are going 
to have to [prove it by construction][2]--which is to say, by 
writing a function (like a constructor or an `opCast` overload) 
that actually performs the conversion.

[1]: https://issues.dlang.org/show_bug.cgi?id=1807
[2]: https://en.wikipedia.org/wiki/Constructive_proof


More information about the Digitalmars-d mailing list