Flatten a range of static arrays

ag0aep6g anonymous at example.com
Fri Feb 7 23:30:28 UTC 2020


On 08.02.20 00:10, nullptr wrote:
> ```
> import std;
> 
> struct SomeRange
> {
>      int[3] val;
> 
>      enum empty = false;
> 
>      auto popFront() @safe {}
> 
>      ref auto front() @safe
>      {
>          return val;
>      }
> }
> 
> void main() @safe
> {
>      SomeRange().take(10).map!((return ref x) => x[]).joiner.writeln;
> }
> ```
> 
> I don't know how applicable this is to your use case, but this code will 
> compile and run under -dip1000.

That shouldn't compile. You have found a hole in DIP 1000.

----
struct SomeRange
{
     int[3] val = [10, 20, 30];
     ref auto front() @safe { return val; }
}

int[] f() @safe
{
     SomeRange sr;
//     return sr.val[]; /* error, as expected */
     return sr.front[]; /* no error, but escapes reference to local */
}

void main() @safe
{
     auto x = f();
     import std.stdio;
     writeln(x); /* Prints garbage. */
}
----

I'm too lazy right now to check if it's already in Bugzilla.


More information about the Digitalmars-d-learn mailing list