Containers

Paul Backus snarwin at gmail.com
Tue Aug 31 18:11:04 UTC 2021


On Tuesday, 31 August 2021 at 17:21:02 UTC, deadalnix wrote:
> For instance, if we have a Vector!T, then we want it to convert 
> to a Vector!(const T) and we want const(Vector!T) == 
> const(Vector!(const T)), all of it implicitly. Same goes for 
> other qualifiers.

As far as I know, the best approach anyone's come up with for 
this is the `headMutable` pattern described in this blog post:

https://dlang.org/blog/2020/06/25/a-pattern-for-head-mutable-structures/

This gives us:

* `Vector!T` -> `const(Vector!T)` (built in)
* `const(Vector!T)` -> `Vector!(const(T))` (via `headMutable`)
* `Vector!(const(T))` -> `const(Vector!(const(T)))` (built in)
* `const(Vector!(const(T)))` -> `Vector!(const(T))` (via 
`headMutable`)

The only conversion missing is

* `const(Vector!(const(T)))` -> `const(Vector!T)`

...which would have to be implemented as another method 
(`tailUnqual`?).

Of course many of these conversions are explicit, not implicit, 
but I don't think there's any way around that given D's current 
capabilities. The only user-defined implicit conversions in D are 
via inheritance and `alias this`, and neither can do what we need 
here.

>  - COW. Value type collection can be very expensive to work 
> with if the eagerly copy everything. On the other hand, COW 
> ensures copies are cheap. There are also very common use case 
> that are well served by COW collections, for instance, when one 
> wants value types for collection that are infrequently changes 
> - but you don't know or can't prove that they won't.

This sounds similar to Clojure's immutable data structures. I 
agree these would be a good thing to have, although I think 
traditional mutable containers should also exist alongside them.


More information about the Digitalmars-d mailing list