Nowadays it’s really really hard if not possible to generalize over tuple type. After reading Nascent Idea: allow `.0`, `.1`, etc on arrays, I’ve come up with this idea:
Imagine there’re traits:
trait TryIndexAs<T> {
fn try_index_as(&self, index: usize) -> Option<&T>;
}
trait TryIndexMutAs<T> {
fn try_index_mut_as(&mut self, index: usize) -> Option<&mut T>;
}
And the compiler somehow managed to implement them for all the tuple types, when index and T corresponding to a position in a tuple, it returns Some, otherwise it will just return None:
impl<A> TryIndexAs<A> for (A,) {
...
}
impl<A> TryIndexAs<A> for (A,A) {
...
}
impl<A,B> TryIndexAs<A> for (A,B) {
...
}
impl<A,B> TryIndexAs<B> for (A,B) {
...
}
So one will be able to use impl TryIndexAs<A> + TryIndexAs<B> to generalize over all tuples consisting of types A and B and other types.
Is this a good idea?