Skip to content

Iterators #8

@weltkante

Description

@weltkante

In your post about safe native code you drop a line about the C# enumerable/enumerator design beign a bad decision and that you had a better solution.

IEnumerator requires two interface dispatches simply to extract the next item! Compare that to C++ iterators which can compile down a pointer increment plus dereference. Many of these problems could be addressed simply with better library designs.

While I agree that MoveNext and Current in C# could be merged into a single call you're not really comparing the same thing are you? Iterators in C++ are the equivalent to the handwritten C# enumerator structs, which could (theoretically) be optimized and inlined pretty well, too, and certainly have no interface dispatches either.

As far as I know C++ doesn't have modular iterators which can be exported from a component and consumed by another. Creating modular code in a simple way is the major advantage of the C# enumeration protocol. As far as I know (but I may be missing something) C++ doesn't have any concept of how to export an iterator from one library and consume it from another which isn't in the same source module. You have to come up with your own interfaces and iteration protocol and if its modular you probably won't get around virtual interface method invocation either, I think. Especially if you combine it with generics and collections.

Our final design for enumeration didn’t even invole interfaces at all.

I'd love to hear a bit more about that, as I cannot imagine how that is possible. Even if its not a full blog post but just some hints at how your iteration design looked like.

Anyways thanks a lot for all these posts, it's awesome to have a peek at what could be possible with different language design. :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions