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

Iterator::collect should ideally support conversions #425

Open
danakj opened this issue Dec 6, 2023 · 1 comment
Open

Iterator::collect should ideally support conversions #425

danakj opened this issue Dec 6, 2023 · 1 comment
Labels
design Design of the library systems as a whole, such as concepts
Milestone

Comments

@danakj
Copy link
Collaborator

danakj commented Dec 6, 2023

An Iterator<const std::string&> can not be collected into a Vecstd::string, because it requires FromIterator<std::string> to be true for Iterator<const std::string&> which it's not. The from_iter() impl needs to accept an IntoIterator<std::string> which is not matching Iterator<const std::string&>'s implementation which returns itself.

An example in subdoc:

  run_options.macro_prefixes =
      sus::iter::from_range(option_include_macro_prefixes)
          .cloned()
          .collect<sus::Vec<std::string>>();

This should really work without cloned() ideally, since it's a copy constructible type.

@danakj danakj added the design Design of the library systems as a whole, such as concepts label Dec 6, 2023
@danakj danakj added this to the Iterators milestone Dec 6, 2023
Ananyasingh2002 pushed a commit to Ananyasingh2002/subspace that referenced this issue Jan 1, 2024
@danakj
Copy link
Collaborator Author

danakj commented Jan 5, 2024

Some thoughts from the PR #425:

The issue is that IntoIterator (and thus collect) requires the into_iter() function's returned Iterator::Item type to be exactly the same as the type specified in IntoIterator. But I believe it would be better here to match an Iterator::Item type if its convertible-to the type in IntoIterator.

Some options:

  • Maybe collect needs to use a more lenient version of IntoIterator, and perform a map itself when the types don't match.
  • Maybe Iterator can match a concrete type if its Item is convertible-to T since next() returning a T will be valid in code that wants something T converts to as well.

Would the latter then automatically make IntoIterator more lenient? I think so, but didn't look carefully yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of the library systems as a whole, such as concepts
Projects
None yet
Development

No branches or pull requests

1 participant