Containers
Paul Backus
snarwin at gmail.com
Wed Sep 1 11:57:01 UTC 2021
On Wednesday, 1 September 2021 at 11:04:41 UTC, Steven
Schveighoffer wrote:
> It's possible to forward, i.e.:
>
> ```d
> template Vector(T) if (isConst!T) { alias Vector =
> tailconst(.Vector!(Unqual!T)); }
> ```
>
> If you look at arrays, `const(T)[]` is the same as
> `tailconst(T[])`. This would be similar.
This runs directly into the issue 1807 problem. The compiler has
to recognize this specific pattern as meaningful, and if the
programmer writes something *semantically* equivalent using a
different pattern, their code will mysteriously break; e.g.,
```d
alias TailConstOf(T) = tailconst(T);
alias Vector(T : const(U)) = TailConstOf!(.Vector!U);
```
> The problem I have with any other approach is that the compiler
> doesn't understand the relationships, and you will run into
> weird corner cases. It can't correctly add or strip the
> qualifier to the correct members when calling compatible
> functions.
With the implicit `opCast`/constructor approach (or the
`opHeadMutable` approach), the compiler doesn't have to
understand the relationships. It just needs to know the original
type and the desired type, and call the appropriate user-defined
function to perform the conversion.
More information about the Digitalmars-d
mailing list