Hello. Just wondering, is it possible to define a subtrait as a version of its supertrait with extra constraints on the supertrait's generic associated type? I'm thinking of something like this, but this seems to be invalid syntax.
You can make the super-trait be more specific.
pub trait ClonableFn<'a, B>: Kind1L2T + Sized
where
<Self as Kind1L2T>::Output<'a, Self, B>:,
{
}
Not sure if it will match what you may be trying to get to.
"extra constraints" in general is a term I would not use describing super-traits; as the purpose of traits is one of extending functionality.
You can have constrained subtraits already. The barriers to your particular example have more to do with the limitations of higher-ranked trait bounds (where for<...>).
Higher-ranked trait bounds over types requires non-lifetime binders (for<T>), which are unstable. You also want conditional binders (for<'a where 'b: 'a>), which don't exist so far (and it's unsure what form they will take). So it will be some time before a constraint close to that will be directly possible (if it does become possible).
There's another problem with your particular example: Kind1L2T requires that Output<'a, A, B> is defined for all 'a, A, and B -- not just when A: 'a and B: 'a. So no implementor can actually meet your bounds, given the GAT in that form.
The poor interaction between GATs and HRTBs is a well known problem. Here's an article about working around those problems for lifetime GATs. There's a section about HRTB implicit bounds, which is a way to emulate conditional binders. However, to be applicable to your use case (with a type GAT), you would also need non-lifetime binders.
It might look something like this:
(I didn't try making actual implementations and testing the bound when calling a function. And as the warning says, that feature is incomplete.)
Edit: The incomplete non-lifetime binder feature doesn't impose an implicit Sized bound on the introduced type generics, which may make it impractical unless something else changes (conditional binders, a way to emulate them for trait bounds, support for implied Sized specifically, ...). But here's the example in action.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.