On Friday, March 23, 2012 23:37:27 Ali Çehreli wrote: > 'in' is the same as 'const scope'. const part is easy to understand but > I don't know what 'scope' does. I could not understand what the spec > means with "references in the parameter cannot be escaped (e.g. assigned > to a global variable)" for scope: > > http://dlang.org/function.html
You mean that you don't understand what it means for a reference to escape a function? One example of this with pointers would be int* func() { int a; return &a; } A pointer to a local variable has escaped the function. That pointer is invalid. A case this simple gets caught by the compiler, but it's very easy to get around that (e.g. passing &a to a function which returns it and then return the result of that function rather than &a directly). In the case of scope, it means that what you pass in cannot be assigned to a global or static variable, and it cannot be returned from that function. So, for instance. int[] foo(const int[] a) { ... } could not assign a to a global or static variable and cannot return it. This can allow some additional optimizations as well as improve safety in some cases. For instance, if you pass int[5] b; to foo, then you know that no slice of that static array will escape, since something like int[] bar() { int[5] c; return c; } is just as bad as the case with &a. I believe that scope is used primarily with delegates however (particularly, since you often _want_ to be able to return a slice of an array passed to a function). It guarantees that the the delegate's context won't escape the function and can make it so that the delegate's context doesn't have to be allocated on the heap, which improves efficiency. scope is pointless with types which aren't pointers or references though. - Jonathan M Davis