Dlist and dip1000 challenge

Stanislav Blinov stanislav.blinov at gmail.com
Tue Oct 23 20:10:36 UTC 2018


On Tuesday, 23 October 2018 at 19:03:25 UTC, Steven Schveighoffer 
wrote:

>> So, if "decomposition may continue transitively", it looks as 
>> if there's no tail-scope for arrays, but then shouldn't it 
>> follow that for
>> 
>> void foo(scope string[] args...);
>> 
>> it should transitively slap `scope` into the immutable(char)* 
>> of each argument?
>
> This is where I think there is going to be a problem. scope 
> shouldn't be transitive, you can easily have a pointer 
> allocated on the stack point to heap data.

Yeah, but OTOH, so can you have an array of pointers to the stack 
:\

> It may be an issue of expressability.

void tailScope(T scope [] x); // yuck...

>> 
>> ....and yet, in your code example the compiler behaves as if 
>> each string was indeed `scope`.
>
> Yes, I believe the compiler inferring scope can do some things 
> that syntax cannot. I think that might be where the issue is, 
> but it's really hard to figure out.

This is weird. 'foreach' compiles, manual iteration doesn't. 
Change the 'manualIter' enum below to compare.

/* Simplified versions of range primitives, for easy reference.
    Note, these don't handle decoding narrow strings!
*/

@property bool empty(T)(T[] a) @safe pure nothrow @nogc {
     return a.length == 0;
}

void popFront(T)(ref T[] a) @safe pure nothrow @nogc {
     assert(!a.empty);
     a = a[1 .. $];
}

/* version in std.range doesn't have a 'return' argument, but 
that doesn't seem to have an effect. */
@property ref T front(T)(return T[] a) @safe pure nothrow @nogc {
     assert(a.length);
     return a[0];
}

struct Array(T) {
     T[] data;

     void append(Stuff)(Stuff stuff) {
         appendImpl(stuff);
     }

     void appendImpl(T[] x...) {
         appendImpl2(x);
     }

     void appendImpl2(Stuff)(ref scope Stuff stuff) {

         // Change this to true to get different behavior
         enum manualIter = false;

         static if (manualIter) {
             while (!stuff.empty) {
                 data ~= stuff.front;
                 stuff.popFront;
             }
         } else {
             foreach (ref e; stuff) data ~= e;
         }
     }
}

void main() {
     Array!string array;
     array.append("hello");
}



More information about the Digitalmars-d mailing list