Make foreach element optional

bachmeier no at spam.net
Tue Mar 16 17:00:09 UTC 2021


On Tuesday, 16 March 2021 at 16:29:45 UTC, Imperatorn wrote:
> On Tuesday, 16 March 2021 at 13:52:29 UTC, Per Nordlöw wrote:
>> On Tuesday, 16 March 2021 at 13:31:34 UTC, Imperatorn wrote:
>>> foreach(0..n) could work. Why though.
>>
>> When performing a side-effect n times.
>
> Then why not just do:
>
> auto times(alias F, T)(T number)
> {
>     return number.iota.each!(_ => F());
> }
>
> void f()
> {
>     writeln("hi");
> }
>
> n.times!(f);
>
> ?

To my knowledge, there's nothing like this in the standard 
library or the language. I used something similar but eventually 
decided it was simpler to use the original foreach. It'd honestly 
have to be a language change to be useful. (Given the benefit, I 
doubt this would happen.)


More information about the Digitalmars-d-learn mailing list