[rust-dev] iter library

Joe Groff arcata at gmail.com
Sat Apr 28 08:02:15 PDT 2012

On Apr 27, 2012, at 11:03 PM, Marijn Haverbeke <marijnh at gmail.com> wrote:

> What Joe meant is that you could simply write multiple impls on the
> same type with different names for the various modes of iteration.
>    impl of iter for maptype { ... }
>    impl iter_keys of iter for maptype { ... }
>    impl iter_vals of iter for maptype { ... }
> You could then do 'import map::iter_keys;` at the top of a block to
> force the key-iterating impl to take precedence there.
> I think the ergonomics of this kind of trick didn't work out as well
> as hoped, though. You'd get multiple applicable impl errors when
> importing `map::*`, and seeing which impl is currently closest in a
> scope is somewhat indirect and confusing.

That is indeed what I was going for, but I suggested it thinking it
was possible to instantiate a named impl explicitly when multiple
impls are in scope. If that's not the case then wrapper types make
sense, but this looks like a prime use case of named impls to me. Are
there problems with allowing impls to be used as a constructor or
cast,  to allow for example `iter_keys(map).each` or
`map.(iter_keys).each` to just work given an impl iter_keys?


More information about the Rust-dev mailing list