Comment #0 by qs.il.paperinik — 2024-06-27T17:38:47Z
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
Comment #1 by iamthewilsonator — 2024-08-26T06:43:53Z
seems like the text in your explanation of the tree walking got cut off
Comment #2 by qs.il.paperinik — 2024-09-26T13:08:15Z
A use case is a recursive `opApply` that walks a tree-like structure; the recursive calls calls `opApply` on the child trees.
Comment #3 by robert.schadek — 2024-12-13T19:36:04Z