www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24635] New: Allow opApply with default parameters

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

          Issue ID: 24635
           Summary: Allow opApply with default parameters
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: qs.il.paperinik gmail.com

Currently, to be used by `foreach`, `opApply` must have exactly 1 parameter. It
cannot have additional parameters with default values:

```d
struct S
{
    int opApply(int delegate(int) dg, int x = 0) => dg(x);
}

void main()
{
    foreach (int x; S()) { } // Error: cannot uniquely infer `foreach` argument
types
    foreach (x; S()) { } // Error: cannot uniquely infer `foreach` argument
types
}
```

Of course, the workaround is to call another function:

```d
struct S
{
    int opApply(int delegate(int) dg) => opApplyX(dg, 0);
    int opApplyX(int delegate(int) dg, int x) => dg(x);
}
```

However, the whole purpose of default arguments is that one need not do that.
The difference between calling into another function or having parameters with
default values becomes bigger for classes when those functions are virtual.

A use case is a recursive `opApply` that walks a tree-like structure; the
recursive calls pass

--
Jun 27