Open Centril opened 6 years ago
Here's my attempt at a rust translation of the place I end up using this the most in C#:
fn get_or_default<'a, type K, type T, type D, trait Tr>
(h: &'a HashMap<K, T>, k: K, d: &'a D) -> &'a dyn Tr
where T: Tr, D: Tr
{
match h.entry(k) {
Occupied(x) => x.value(),
Vacant => d,
}
}
So, supposing container traits, you could &dyn Stack from a Vec if you wanted, but you could also use Tr = bikeshed Vec<i32>
to get a real Vec out.
You gave me an idea...! You've ensured that two possibly different types both implement the same trait without mentioning the trait. I wonder if that can be useful on its own?
MWE:
fn assert_both_same_trait<trait Tr, A: Tr, B: Tr>() {}
assert_both_same_trait::<Sync, i32, u32>();
What would this mean?