I'm not sure why your cannotInfer examples fail, but canInfer3 and canInfer4 succeed because the parameter (This bar) in the lambda is inferred scope since it's passed by value and the lambda is `pure` and returns `void`, so it can't escape `arr`. The `scope` annotation of canInfer3 doesn't do anything since it's not a `delegate` but a `function` since you pass `this` explicitly through parameter `bar`.
Comment #2 by jlourenco5691 — 2021-09-07T17:12:23Z
Yes the last 2 make sense to infer scope, the comments were because I'm specifying other templates with `(this This)` and they cannot infer scope.
> but canInfer3 and canInfer4 succeed because the parameter (This bar) in the lambda is inferred scope since it's passed by value and the lambda is `pure` and returns `void`, so it can't escape `arr`.
Ok, but I can make it a function and still escape its context:
```
@safe:
struct Bar
{
void canInfer(this This)()
{
This other;
(This bar) { other = bar; } (this);
with(other) cast(void) &i;
}
int[] arr = [0];
int i;
}
void main()
{
scope bar = Bar();
bar.canInfer;
}
```
Comment #3 by jlourenco5691 — 2021-09-07T17:15:02Z
I meant a delegate.
Comment #4 by robert.schadek — 2024-12-13T19:18:22Z