DIP1000: Scoped Pointers

Meta via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Tue Aug 16 11:25:42 PDT 2016


On Tuesday, 16 August 2016 at 16:34:05 UTC, Dicebot wrote:
> On 08/16/2016 07:26 PM, Patrick Schluter wrote:
>> What happens in that case ?
>> 
>> void test() {
>> scope rnd  = new Rnd; // reference semantic and stack allocated
>> Rnd rnd2;
>>  rnd2 = rnd;
>>          some_sneaky_function_that_saves_global_state(rnd);
>> }
>> 
>> or is that not even possible ? (sorry I'm still a noob in D).
>
> If `Rnd` is supposed to be a class, it won't compile because it 
> would
> mean escaping scope reference to non-scope variable (with 
> DIP1000). If
> it is a struct, it won't compile because you are trying to 
> assign `Rnd*`
> (pointer) to `Rnd` (value) :)

What about this?

struct Rnd
{
     int* state;
}

void test()
{
     scope rnd = new Rnd();
     Rnd rnd2 = *rnd;

     saveGlobalState(rnd2);
}


More information about the Digitalmars-d-announce mailing list