Cannot take slice of scope static array in @safe code

Steven Schveighoffer schveiguy at gmail.com
Sun Feb 2 19:13:28 UTC 2020


On 2/2/20 1:57 PM, ag0aep6g wrote:
> On 02.02.20 19:49, ag0aep6g wrote:
>> On 02.02.20 19:18, Steven Schveighoffer wrote:
>> I'm not sure if I got it right, but like this?
>>
>>      int*[][] g1;
>>      int*[] g2;
>>      int* g3;
>>
>>      void main() @safe
>>      {
>>          /* An array stored on the stack, of references to heap data: */
>>          int*[3] a1 = [new int, new int, new int];
>>          /* Another such array: */
>>          int*[3] a2 = [new int, new int, new int];
>>          /* An array of those arrays, stored on the stack: */
>>          int*[][2] b = [a1[], a2[]];
>>
>>          g1 = b[]; /* Nope. */
>>          g2 = b[0]; /* Nope. */
>>          g3 = b[0][0]; /* Ok. */
>>      }
> 
> I guess I kinda missed the point there, and what you want is this:
> 
>      scope int*[][] slice = b[];
>      /* ... use `slice` in non-leaky ways ... */
> 
> Which DIP 1000 doesn't allow, because it wouldn't be able to ensure that 
> the references to a1 and a2 don't leak. Yeah, DIP 1000 falls somewhat 
> short.

The simple problem I've had is (I think), I want an array of strings 
that is scope as a parameter (i.e. I won't share any of the addresses of 
those elements outside the function), but the strings themselves are 
strings that are usable anywhere.

I can't figure out where I've seen this, or how to reproduce it (writing 
a simple test seems to work). But I've had it happen to me.

That's the problem with dip1000. It works in most cases, and then you 
get this one tricky case, and you can't figure out how to get around it. 
Or you have to do everything in one expression so the compiler can 
deduce the actual scope-ness of things you aren't allowed to declare. It 
leaves a general feeling of something missed in the design.

-Steve


More information about the Digitalmars-d-learn mailing list