www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - scope in function argument

reply Freddy <Hexagonalstar64 gmail.com> writes:
What does it mean when there is a scope in a function argument.
---
void func(scope int* a){}
---
Sep 23 2015
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 23 September 2015 at 17:09:40 UTC, Freddy wrote:
 What does it mean when there is a scope in a function argument.
That you are not supposed to let that reference escape the function scope. The compiler does little verification of this right now, but may optimize on that assumption (notably, scope delegates will not be copied to the heap right now). In the future, it may become an error to allow them to escape. int* b; void func(scope int* a) { b = a; // you made a escape the function scope, undefined behavior results }
Sep 23 2015
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 09/23/2015 10:11 AM, Adam D. Ruppe wrote:
 On Wednesday, 23 September 2015 at 17:09:40 UTC, Freddy wrote:
 What does it mean when there is a scope in a function argument.
That you are not supposed to let that reference escape the function scope.
Just to complete with a related feature, here are also 'return' parameters, which make the compiler ensure that the returned reference lives longer than the 'ref' argument. Unfortunately, I can't see that feature here anymore: http://dlang.org/function.html#parameters Has it been pulled back? It still works in 2.068. I had written about it: http://ddili.org/ders/d.en/function_parameters.html#ix_function_parameters.return,%20parameter Then there is the -dip25 compiler switch that forces the programmer to put 'return' on every returned 'ref' parameter, effectively enforcing that check: http://wiki.dlang.org/DIP25 Ali
Sep 23 2015