Make std.container.Array an output range

monarch_dodra via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 5 09:29:08 PDT 2014


On Thursday, 5 June 2014 at 16:21:21 UTC, monarch_dodra wrote:
> Well, one issue is that for a "Range", "put" really just means 
> overwrite the front element, and pop it. So...
>
> Array!int myArray = ...:
> copy([1, 2, 3], myArray);   //(1)
> copy([1, 2, 3], myArray[]); //(2)
>
> In this situation, (1) and (2) would have different meaning.

Just to clarify, I think that means that MAKING Array an output 
range is a bad idea, for the same reasons that vector is not an 
output iterator.

That said, having an explicit OutputRange adapator:

struct BackInserter(Container)
{
     Container* c;
     void put(T)(ref T t)
     {
         c.insertBack(t);
     }
}
auto backInserter(Container)(ref Container c)
{
     return BackInserter!Container(&c);
}


Useage:
inputRange.copy(someArray.backInserter());

There: Clear and un-ambiguous.

>> Should I file an enhancement request or is there something 
>> fundamentally wrong with this idea? For Array it should be as 
>> simple as adding
>>
>> alias doPut = insertBack;

doPut is not a range primitive. Implementing "doPut" on your 
container should have no effect. If it does, it is a bug and will 
*quickly* be fixed. Don't count on it working.

The correct primitive is "put".


More information about the Digitalmars-d mailing list