Questions on the new __traits(parameters)

Steven Schveighoffer schveiguy at gmail.com
Thu Mar 10 15:08:11 UTC 2022


On 3/9/22 5:04 AM, Quirin Schroll wrote:
> In the changelog it says:
>> When used inside a foreach using an overloaded `opApply`, the trait 
>> yields the parameters to the delegate and not the function the foreach 
>> appears within.
> 
> Why? This will rarely be wanted or be used intentionally. When a 
> programmer uses `__traits(parameters)` in a `foreach` loop, it will for 
> certain happen to someone not aware of this. The iteration implemention 
> (`opApply` or range functions) is a detail one should not really have to 
> care about on the usage side. This complicates the language. Morally, 
> this is a bug. Please reconsider this design decision before it sticks.

Yes, I agree.

```d
string s = "WTF";

alias p = typeof(__traits(parameters));
foreach(x; s) {
    static assert(__traits(isSame, p, typeof(__traits(parameters))));
}

foreach(dchar x; s) {
    static assert(!__traits(isSame, p, typeof(__traits(parameters))));
}
```

Lowering should be invisible to the plain language. This is similar to 
issues where lowered code has errors and you get error messages on code 
that you never wrote.

Imagine if inside an opApply delegate, a return statement just returned 
the delegate. This is equivalent to what you are saying here.

-Steve


More information about the Digitalmars-d mailing list