On 8/14/2016 7:37 AM, Dicebot wrote:
struct Container
{
int data;
static struct Range
{
int* pdata;
// range methods skipped for clarity ..
}
Range asRange ( )
{
return Range(&this.data);
}
}
void main ( )
{
Container container;
import std.stdio;
writeln(container.asRange()); // case 1, OK
scope r = container.asRange(); // case 2, OK
auto r = container.asRange(); // case 3, not OK
}
A great example. Let's analyze by peeling back the syntactic sugar:
----
'container' is a stack variable
'container.int' is a stack variable
'asRange()' returns an instance of 'Range' which is a stack variable, call it
'range'
'range.pdata' is a stack variable
Range range.pdata = &container.data; // range is inferred as 'scope' because
// container.data is a stack variable
auto r = range; // r is inferred as 'scope' because 'range' is 'scope'
----
It's a bit advanced, but it falls entirely in the DIP rules and is workable with
some implementation effort (not sure how much).
The idea is, if something is ultimately a stack variable, the scope rules apply.