Creating a custom iota()

ag0aep6g anonymous at example.com
Thu May 12 19:51:35 UTC 2022


On Thursday, 12 May 2022 at 17:06:39 UTC, Ali Çehreli wrote:
> void main() {
>   const st = DateTime(Duration(0));
[...]
>
>   // (0) I think D should not insist on 'const'
>   // when copying types that have no indirections.
>   // We shouldn't need the cast() below in this case.
[...]
>   iota(cast()st, en, step).each!writeln;
> }
>
[...]
> auto iota(B, E, S)(B begin, E end, S step)
[...]
> {
>     static struct Result
>     {
>         B current;
[...]
>         void popFront()
>         {
[...]
>             current += step;
>         }
>     }
[...]
> }

Mark iota's `begin` parameter as const. Then you don't need the 
cast, because `B` will be mutable.


More information about the Digitalmars-d-learn mailing list