www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is 'scope' in function parameter?

reply Sobaya <sobaya007 gmail.com> writes:
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?
Dec 25 2017
next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Dec 25 2017
parent reply Mike Franklin <slavo5150 yahoo.com> writes:
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
Dec 25 2017
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Dec 27 2017
prev sibling parent reply Mike Franklin <slavo5150 yahoo.com> writes:
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
Dec 25 2017
parent Sobaya <sobaya007 gmail.com> writes:
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
Dec 26 2017