Transforming a range back to the original type?

Steven Schveighoffer schveiguy at yahoo.com
Fri May 4 06:22:44 PDT 2012


On Fri, 04 May 2012 09:06:31 -0400, Jakob Ovrum <jakobovrum at gmail.com>  
wrote:

> On Friday, 4 May 2012 at 12:49:35 UTC, Steven Schveighoffer wrote:
>> or more generically:
>>
>> c = new typeof(c)(c.filter!(x => x < 3));
>>
>> ?
>
> That doesn't do the right thing when 'c' isn't a class type, so it's not  
> really that generic.
>
> Struct types (the type of `new typeof(c)` is not `typeof(c)`!) and array  
> types simply fail with your suggestion.

Structs don't make very good containers.  A slice is not really a  
container.

But in any case, someone can make a function that's generic with a couple  
static ifs.

> I personally haven't decided whether I think a generic convention is  
> useful here, but that code doesn't fit the bill.

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


More information about the Digitalmars-d-learn mailing list