DIP1000

Loara loara at noreply.com
Fri Jun 24 17:53:07 UTC 2022


On Thursday, 23 June 2022 at 16:08:01 UTC, Ola Fosheim Grøstad 
wrote:
> How am I supposed to write this:
>
> ```d
> import std;
> @safe:
>
> struct node {
>     node* next;
> }
>
> auto connect(scope node* a, scope node* b)
> {
>     a.next = b;
> }
>
> void main()
> {
>     node x;
>     node y;
>     connect(&x,&y);
> }
>
> ```
>
>> Error: scope variable `b` assigned to non-scope `(*a).next`

Why you should use `scope` here? A `scope` pointer variable may 
refer to a stack allocated object that may be destroyed once the 
function returns.

Since a linked list should not contain pointers to stack 
allocated data you should avoid entirely the `scope` attribute 
and use instead `const`.




More information about the Digitalmars-d-learn mailing list