[Issue 24635] New: Allow opApply with default parameters
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Jun 27 17:38:47 UTC 2024
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 at puremagic.com
Reporter: qs.il.paperinik at 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
--
More information about the Digitalmars-d-bugs
mailing list