Hello
Firstly, for context:
I want to do make a type-level type map, similar to tlist
as a list, but where I have ((((), A), B), A)
and then can retrieve the outermost &A
by going through the tuple from outer to inner, checking if (_, T); T = A
and then reference that, elsewise go down the tuple tree.
For a dummy implementation with specialization, that would look like this:
trait TMap<T> {
fn get(&self) -> &T;
}
impl<Down, T> TMap<T> for (Down, T) {
fn get(&self) -> &T {
&self.1
}
}
impl<Down: TMap<T>, T, Other> TMap<T> for (Down, Other) {
fn get(&self) -> &T {
self.0.get()
}
}
the only problem is that this makes a conflicting implementation between type (_, _)
and (_, _)
.
Now I know that specialization is a year-, almost decade-long complicated and discussion heavy topic, which shouldn't be just half-ready implemented, so I went on a way to see if I can get around that.
I won't mention the other things I tried here, as they all lead to other forbidden or not-yet-ready implemented features. If you want to know them, let me know.
The thing I'm interested in the most is with this variant:
trait Is<T> {}
impl<T> Is<T> for T {}
trait TMap<T> {
fn get(&self) -> &T;
}
impl<Down, T> TMap<T> for (Down, T)
where
T: Is<T>,
{
fn get(&self) -> &T {
&self.1
}
}
impl<Down: TMap<T>, T, Other> TMap<T> for (Down, Other)
where
Other: !Is<T>,
{
fn get(&self) -> &T {
self.0.get()
}
}
here, I want to convince the compiler that the second type in the tuple is concretely not implementing the trait which the other impl block requires so that they don't conflict.
Tho this doesn't work, I get the error negative bounds are not supported
.
And finally, my simple question: Why? Is there any specific reason for that? Something that makes it hard to implement? Is there some Pre-RFC already? (at least I couldn't find one after a quick search here in the forum) Would this be something to RFC?
Thank you for reading this mess of thoughts and hacky stuff