Flatten a range of static arrays

Steven Schveighoffer schveiguy at gmail.com
Sat Feb 8 00:17:07 UTC 2020


On 2/7/20 6:30 PM, ag0aep6g wrote:
> 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.

The original code is not invalid though. f is not valid, and that is a 
bug, but the original code posted by nullptr should be fine by memory 
safety standards.

If there is no possible way to do the original code with dip1000 
attributes, then that's a bug with dip1000's design (would not be 
surprised).

-Steve


More information about the Digitalmars-d-learn mailing list