std.array.array seems to return flatten copy of input

Olivier Grant against.the at evil.bots.com
Fri Apr 4 13:35:57 PDT 2014


That is one responsive D community :) Thank you to all three of 
you for these quick answers.

I was really scratching my head on that one. This shows that UFCS 
can be quite dangerous. Is there any way to hide Splicer.array to 
the outside world? It just seems that it would be very easy to 
break client code by updating your implementation if you end up 
using a name that is already used elsewhere and accessed using 
UFCS.


Thanks,

O.

On Friday, 4 April 2014 at 20:29:28 UTC, anonymous wrote:
> On Friday, 4 April 2014 at 20:19:53 UTC, Olivier Grant wrote:
>> import std.stdio;
>> import std.range;
>> import std.array;
>>
>> auto splice( size_t N, R )( R range )
>>   if(isInputRange!R)
>> {
>>   struct Splicer
>>   {
>>      R array;
>>
>>      @property bool empty( ) const
>>      { return 0 == array.length; }
>>
>>      @property auto front( ) const
>>      { return array[0 .. N]; }
>>
>>      void popFront( )
>>      { array = array[N .. $]; }
>>   }
>>
>>   static assert(isInputRange!Splicer);
>>
>>   assert(range.length % N == 0);
>>
>>   Splicer res = { range };
>>   return res;
>> }
>>
>> unittest
>> {
>>   assert(equal([1,2,3,4].splice!(2), [[1,2],[3,4]]));
>> }
>>
>> void main( )
>> {
>>   auto a = [1,2,3,4];
>>
>>   writeln(a.splice!(2));
>>   writeln(a.splice!(2).array);
>> }
>>
>> which weirdly enough gives me the following output :
>>
>> [[1,2],[3,4]] // I expect that.
>> [1,2,3,4] // But what is happening here ?
>
> a.splice!(2).array is the field of the Splicer struct. Rename
> that or call std.array.array not via UFCS.



More information about the Digitalmars-d-learn mailing list