[Issue 23666] Recognize template opApply pattern

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 29 10:15:49 UTC 2024


https://issues.dlang.org/show_bug.cgi?id=23666

--- Comment #3 from Bolpat <qs.il.paperinik at gmail.com> ---
(In reply to Nick Treleaven from comment #2)
> You can use `auto opApply` to infer attributes instead of a template.

```d
struct S
{
    auto opApply(int delegate(ref int) callback)
    {
        int x;
        return callback(x);
    }
}

void main() @safe
{
    foreach (ref x; S()) {} // Error: `@safe` function `D main` cannot call
`@system` function `S.opApply`
}
```

However, this does (for reasons beyond my understanding):
```d
struct S
{
    int opApplyImpl(DG : int delegate(ref int))(scope DG callback)
    {
        int x;
        return callback(x);
    }
    alias opApply = opApplyImpl!(int delegate(ref int));
}

void main() @safe
{
    foreach (ref x; S()) {}
}
```

--


More information about the Digitalmars-d-bugs mailing list