How to replicate an Error: scope variable `old` assigned to `ref` variable `ctx` with longer lifetime?

Dennis dkorpel at gmail.com
Tue May 13 12:08:09 UTC 2025


On Tuesday, 13 May 2025 at 11:55:21 UTC, matheus wrote:
> Unfortunately adding the pointer gives me another error:

I added @safe: and replaced `const` with `auto` because casting 
away const isn't safe. This should do it:

```D
// compile with -preview=dip1000
import std;

@safe:

struct S { int* p; }

void f(ref scope S ctx) {
     auto old = ctx;
     g(ctx);
     ctx = old;
}

void g(ref scope S ctx) {
     S a;
     ctx = a;
}

void main() {
    S i;
    f(i);
}
```


More information about the Digitalmars-d-learn mailing list