Worst ideas/features in programming languages?
Steven Schveighoffer
schveiguy at gmail.com
Tue Dec 21 17:12:56 UTC 2021
On 12/18/21 2:54 PM, Quirin Schroll wrote:
> On Monday, 15 November 2021 at 14:19:00 UTC, Steven Schveighoffer wrote:
>> On 11/15/21 7:41 AM, Timon Gehr wrote:
>>> In particular, there is `static foreach_reverse`.
>>
>> Although it's fallen mostly out of style, the reason I hated
>> `foreach_reverse` is because it used to be applicable to delegate
>> iteration, but just iterates forward (I've just checked and it's
>> deprecated, but not disabled).
>
> Maybe this is older. When I came to D around 2015, I learned that there
> is `opApply` for forward iteration (`foreach`) and `opApplyReverse` for
> reverse iteration (`foreach_reverse`). I just tried defining a struct
> with `opApply`, but no `opApplyReverse`, and then `foreach_reverse` on
> an instance (on run.dlang.io). Doesn't work.
You can foreach an appropriately typed delegate too.
e.g.
```
struct S
{
int byKey(int delegate(int) dg)
{
return 0;
}
}
void main()
{
S s;
foreach(i; &s.byKey) {}
foreach_reverse(i; &s.byKey) {} // Deprecated, still iterates forwards
}
```
https://run.dlang.io/is/G3KUfl
As I said, it's fallen out of style. I used this [in my dcollections
library](https://github.com/schveiguy/dcollections/blob/82118cfd4faf3f1ad77df078d279f1b964f274e7/concepts.txt#L81-L100)
though.
-Steve
More information about the Digitalmars-d
mailing list