Thread overview
What is 'scope' in function parameter?
Dec 25, 2017
Sobaya
Dec 25, 2017
Jonathan M Davis
Dec 25, 2017
Mike Franklin
Dec 27, 2017
Jonathan M Davis
Dec 26, 2017
Mike Franklin
Dec 26, 2017
Sobaya
December 25, 2017
What means 'scope' in function parameter?

I made a test code.

```
import std.stdio;

int[] x;

void func(scope int[] a) {
    x = a;
}

void main() {
    func([0,1,2]);
    writeln(x);
}
```

This code was successfully compiled and printed '[0, 1, 2]'.

But according to https://dlang.org/spec/function.html, above code must cause a compile error.

Could you give me any advice?
December 25, 2017
On Monday, December 25, 2017 10:42:55 Sobaya via Digitalmars-d-learn wrote:
> What means 'scope' in function parameter?
>
> I made a test code.
>
> ```
> import std.stdio;
>
> int[] x;
>
> void func(scope int[] a) {
>      x = a;
> }
>
> void main() {
>      func([0,1,2]);
>      writeln(x);
> }
> ```
>
> This code was successfully compiled and printed '[0, 1, 2]'.
>
> But according to https://dlang.org/spec/function.html, above code must cause a compile error.
>
> Could you give me any advice?

At this point, scope on function parameters does almost nothing. It's used with delegates to indicate that they don't escape the function so that the compiler can avoid making it so that a closure is allocated. Walter has been working on DIP 1000, which broadens scope so that it affects a lot more types, and that can be triggered with the -dip1000 compiler flag, but it's very much a work in progress, and I wouldn't advise using it at this point. Without it, scope has zero effect on something like a dynamic array.

https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md

- Jonathan M Davis

December 25, 2017
On Monday, 25 December 2017 at 11:09:25 UTC, Jonathan M Davis wrote:

>> ```
>> import std.stdio;
>>
>> int[] x;
>>
>> void func(scope int[] a) {
>>      x = a;
>> }
>>
>> void main() {
>>      func([0,1,2]);
>>      writeln(x);
>> }
>> ```
>>
>> This code was successfully compiled and printed '[0, 1, 2]'.
>>
>> But according to https://dlang.org/spec/function.html, above code must cause a compile error.
>>
>> Could you give me any advice?
>
> Walter has been working on DIP 1000, which broadens scope so that it affects a lot more types, and that can be triggered with the -dip1000 compiler flag, but it's very much a work in progress, and I wouldn't advise using it at this point. Without it, scope has zero effect on something like a dynamic array.
>
> https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md

But we need people to use -dip25 and -dip1000 and provide feedback, submit bug reports, etc.. so we can move the implementation forward.

Based on your assessment, is Sobaya's test case an indication of a bug in -dip1000's implementation?

Mike


December 26, 2017
On Monday, 25 December 2017 at 10:42:55 UTC, Sobaya wrote:

> ```
> import std.stdio;
>
> int[] x;
>
> void func(scope int[] a) {
>     x = a;
> }
>
> void main() {
>     func([0,1,2]);
>     writeln(x);
> }
> ```
>
> This code was successfully compiled and printed '[0, 1, 2]'.
>
> But according to https://dlang.org/spec/function.html, above code must cause a compile error.
>

After a few hours trying to figure out why the compiler didn't catch this, I finally figured it out.  You have to add `@safe`.

import std.stdio;

int[] x;

void func(scope int[] a) @safe
{
    x = a;
}

void main() @safe {
    func([0,1,2]);
    writeln(x);
}

This is one of the things really ticks me off about D; it has all the wrong defaults.

At a minimum, the documentation needs clarification.  I encourage you to file a  bug report against the documentation at http://issues.dlang.org/

Mike

December 26, 2017
On Tuesday, 26 December 2017 at 00:17:33 UTC, Mike Franklin wrote:
> After a few hours trying to figure out why the compiler didn't catch this, I finally figured it out.  You have to add `@safe`.
>
> import std.stdio;
>
> int[] x;
>
> void func(scope int[] a) @safe
> {
>     x = a;
> }
>
> void main() @safe {
>     func([0,1,2]);
>     writeln(x);
> }
>
> This is one of the things really ticks me off about D; it has all the wrong defaults.
>
> At a minimum, the documentation needs clarification.  I encourage you to file a  bug report against the documentation at http://issues.dlang.org/
>
> Mike

Oh! Surely adding @safe makes the expected behavior.

According to your advice, I try to post a bug report.

Thanks!

Sobaya
December 27, 2017
On Monday, December 25, 2017 12:11:58 Mike Franklin via Digitalmars-d-learn wrote:
> But we need people to use -dip25 and -dip1000 and provide feedback, submit bug reports, etc.. so we can move the implementation forward.

Anyone who wishes to play around with those flags is free to do so, and bug reports are certainly welcome, but DIP 1000 is very much a work in progress, and Phobos doesn't work with it yet, so in most cases, it really doesn't make sense to do much with it yet - especially if you're trying to get real work done as opposed to just messing around with a hobby project.

- Jonathan M Davis