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`.


  • DIP1000 Ola Fosheim Grøstad via Digitalmars-d-learn
    • Re: DIP1000 ag0aep6g via Digitalmars-d-learn
      • Re: DIP1000 Ola Fosheim Grøstad via Digitalmars-d-learn
        • Re: DIP1000 ag0aep6g via Digitalmars-d-learn
          • Re: DIP1000 Ola Fosheim Grøstad via Digitalmars-d-learn
          • Re: DIP1000 Ola Fosheim Grøstad via Digitalmars-d-learn
            • Re: DIP... Paul Backus via Digitalmars-d-learn
              • Re:... Ola Fosheim Grøstad via Digitalmars-d-learn
                • ... Dukc via Digitalmars-d-learn
                • ... Ola Fosheim Grøstad via Digitalmars-d-learn
    • Re: DIP1000 Loara via Digitalmars-d-learn
      • Re: DIP1000 Ola Fosheim Grøstad via Digitalmars-d-learn
        • Re: DIP1000 Loara via Digitalmars-d-learn
          • Re: DIP1000 Ola Fosheim Grøstad via Digitalmars-d-learn
            • Re: DIP... bauss via Digitalmars-d-learn
              • Re:... Ola Fosheim Grøstad via Digitalmars-d-learn
            • Re: DIP... Loara via Digitalmars-d-learn
              • Re:... Ola Fosheim Grøstad via Digitalmars-d-learn
                • ... Loara via Digitalmars-d-learn
                • ... Ola Fosheim Grøstad via Digitalmars-d-learn

Reply via email to