Safe Regions with Deterministic Destruction

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Tue Oct 27 18:35:40 UTC 2020


On Tuesday, 27 October 2020 at 18:24:47 UTC, ag0aep6g wrote:
> On Tuesday, 27 October 2020 at 16:56:40 UTC, Ola Fosheim 
> Grøstad wrote:
>> Yes, what I meant is that it is a value on the stack. But 
>> there is a disconnect with classes. "oddball" passes as a 
>> class, even though it would not pass as a struct:
>>
>> class A { int x; }
>> class B { A a; }
>>
>> A oddball() {
>>     scope B b = new B;
>>     b.a = new A;
>>     return b.a;
>> }
>
> `oddball` is neither a class nor a struct; it's a function.

Yes, obviously, but it it passes as a class.

> If you change both `A` and `B` to structs and update the code 
> to `A*` and `B*` where needed, then it works the same as with 
> classes.

Well, but "scope B b = new B;" will stack allocate, right? So it 
is allocated on the stack like a struct. I suspect a user would 
expect b.a to be restricted as it would have been as a struct.

But yes, I get the "one indirection" thing you mentioned. The 
question is, is that useful enough? Is that something users will 
understand?


> It doesn't? The last line gives "Error: scope variable `b` may 
> not be returned". Remember to compile with `-preview=dip1000`.

You are right, the compile switch was missing.

> I'm not sure if I understand the question, but as far as I 
> understand `scope` locals and `scope` parameters work the same 
> for all types. (Maybe there are subtle differences in life

Except that "scope B b = new B" stack allocates? Or does that 
happen for parameters too? Does this cause stack allocation of B?

   f(scope B b){…}

   f(new B);






More information about the Digitalmars-d mailing list