On 5/30/13 8:40 AM, Tommy M. McGuire wrote:
On 05/30/2013 04:56 AM, Niko Matsakis wrote:
Another example is in Hashmaps, where we offer two variants on the
"find-or-insert" pattern:

     fn find_or_insert(&mut self, K, V) -> Option<&V>
     fn find_or_insert_with(&mut self, K, &fn(&K) -> V) -> Option<&V>

Under my proposal there would just be:

     fn find_or_insert(&mut self, K, &fn(&K) -> V) -> Option<&mut V>
     fn find_or_insert_zero(&mut self, K) -> Option<&mut V> // where V:Zero

Thoughts?

I like the idea of the closure, but (as a relative newcomer), how would
the Zero trait be specified? Wouldn't it require all HashMap's V's
implement Zero?

You can add additional bounds to type arguments for a subset of the methods. Use a different impl block:

    impl<K,V> HashMap<K,V> {
        ... find_or_insert goes here ...
    }
    impl<K,V:Zero> HashMap<K,V> {
        ... find_or_insert_zero goes here ...
    }

Patrick

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to