Skip to content

Document other approaches (and why every approach currently sucks a bit) #7

@kitlith

Description

@kitlith

Motivation: I got really excited reading https://blog.weiznich.de/blog/eurorust-non-overlapping-contains-for-hlists/, thinking I had a solution to make this stable. Then I realized that there's still a missing piece, and it boils down not having a proper type equality conditional, which I vaugely remember already knowing would solve my issue.

I have no issue with the core approach to the problem taken in the blog post, but there's a bit I missed on my first read through:

Now we need to provide implementations of our Has trait for all relevant types which are used as type argument to TList. This are the types FeatureA, FeatureB, () in our example.

impl Has<FeatureA> for FeatureA {
    type HasIt = Yes;
}

impl Has<FeatureA> for FeatureB {
    type HasIt = No;
}

impl Has<FeatureA> for () {
    type HasIt = No;
}

The implication here is that it requires number of types in list * number of types you want to be able to check for manual or generated impl blocks, and we can't use generics without falling back to specialization. This is feasible for small numbers of types, little less so if you want to be able to check for every type in the list (becomes n^2 implementations), and not really possible for generic collection types like this one is supposed to be.

To clarify what I mean by "proper type equality conditional": I believe what is needed is something that is allowed to give a false result. We can fairly easily make a trait that tests for type equality (trait Is<T> {}; impl<T> Is<T> for T {}) but the only thing we can do with that in the type system is unconditionally require it to be implemented. We need a true/false output, and the ability to do whatever we want in either case. If we had that, I'd write a trait bound something like where Eq<Input, T>::Res: Or<<Rest as Has<T>>::Res> and be done.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions