Transforming a range back to the original type?
Jakob Ovrum
jakobovrum at gmail.com
Fri May 4 08:21:06 PDT 2012
On Friday, 4 May 2012 at 13:22:47 UTC, Steven Schveighoffer wrote:
> Structs don't make very good containers. A slice is not really
> a container.
Most containers in std.container are structs. A struct can do
everything a class can do, for example, you can choose whether to
use reference semantics or not. So I don't think the assertion
that structs aren't good for containers is true, when they can do
so much more than classes. There is still debate whether
std.container's containers should be classes or structs, but even
so, third-party containers might have different requirements.
> What exactly are you looking for? I mean, you want the
> original type back, so you can reassign, is that all?
> Something like this should work for that:
>
> c = makeContainer!typeof(c)(c.filter!(x => x < 3));
>
> Implementation left as an exercise.
>
> -Steve
It would obviously be a dead simple function, the question is
whether it's generally useful enough for the standard library,
which I personally don't see it being, at least not until I see
at least one good example. I was just pointing out the fact that
it would require a function to abstract away the construction for
all reasonable containers T.
You would also have to decide on an explicit convention of
construction when T is a user-defined type (e.g. define a
constructor taking an input range), as such a function would
effectively formalise it.
(By the way, the template instantiation shortcut syntax only
works when the only argument has exactly one token; your example
code has an error.)
More information about the Digitalmars-d-learn
mailing list