Iterators and Ranges: Comparing C++ to D to Rust

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Tue Jun 15 10:04:03 UTC 2021


On Tuesday, 15 June 2021 at 09:42:10 UTC, Paulo Pinto wrote:
> That short summary doesn't cover the implementation details for 
> all use cases, hence the longwinded articles about their uses 
> in Windows, and C++/WinRT runtime.

This is all interesting, but not really necessary to discuss 
iterators and for-loop like usage.

What is interesting though, is an implementation strategy of 
tearing up a function into many smaller functions so that the 
backend doesn't have to change in order to support it.

So basically:

```
     coroutine(){
        int n = 0;
        while(true){
           n++;
           yield n;
           if (n > 100) {
             yield 0;
           }
        }
     }

```


can be distilled into something like this (unoptimized):


```
     struct {
         int n;
         int result;
         delegate nextstate = state1;
         state1() {
            n++;
            result = n;
            nextstate = state2;
         }
         state2() {
            if (n > 100) {
                result = 0;
                nextstate = state1;
            }
            else state1();
         }
     }

```



More information about the Digitalmars-d mailing list