What is 'scope' in function parameter?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Dec 25 11:09:25 UTC 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



More information about the Digitalmars-d-learn mailing list