Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

immutable TreeMap cannot be iterated #4

Open
ishax-kos opened this issue Jul 21, 2022 · 2 comments
Open

immutable TreeMap cannot be iterated #4

ishax-kos opened this issue Jul 21, 2022 · 2 comments

Comments

@ishax-kos
Copy link

It appears to be impossible to iterate a tree map that is immutable using a foreach loop. The only option seems to be casting away the immutability.

import std.stdio;
import collections.treemap;


auto func() {
    TreeMap!(string, int) foo;
    foo["foo"] = 1;
    return foo;
}


void main() {
    immutable TreeMap!(string, int) tree = func();

    foreach(k, ref v; tree) { // Try with different iteration variables
        writefln!"%s:%s"(k, v);
    }
}
@ZILtoid1991
Copy link
Owner

I guess the issue is that the opApply function overrides lack both the const and immutable attributes. I'll try adding them and see if it creates a problem or not, then hopefully publish a new version with the fix.

@ZILtoid1991
Copy link
Owner

I guess I can add it in the future, but it's not as an easy fix, as modifying the makeFunc functions to generate templates with const and immutable attributes.

One issue is that when I designed this library, I didn't had functional programming in mind. I usually use a mixture of all kinds, using the strength of all various paradigms without overcomplicating things.

Feel free to modify the code if you have the knowledge. Functional programming might even require modifier functions, that add or subtract from the collections by returning a new collection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants