Has there ever been serious discussion around introducing a simpler syntax for at least the majority of cases for associated-type projections? (I've tried some initial searching here but came up empty; I also recognize discussions may be on GitHub, Zulip, or elsewhere).
The Chalk book has a pretty good introduction to associated type projections, for reference: Type equality and unification
The particular thing I'm thinking about is that right now associated types can either be specified with the short syntax MyType::MyAssociatedType
or, if that's insufficient, the much more verbose <MyType as SomeTrait>::MyAssociatedType
.
Readability problems arise when you end up with multiple layers of these associated type projections, which causes increasing nesting of as
-clauses and path segments for each successive associated type.
If, for example, you need to specify a trait bound on some multiply-chained associated-type projection, you end up with something unwieldy like (copied from the libp2p crate):
<<<THandler as IntoProtocolsHandler>
::Handler as ProtocolsHandler>
::OutboundProtocol as OutboundUpgrade<SubstreamRef<Arc<TMuxer>>>>
::Error : Send
One of the key challenges of this syntax is the nesting of <
and >
brackets to contain the Type as Trait
clauses. This makes it difficult to read this syntax left-to-right. I think in general this lack of readability is a barrier to users and writers of Rust code with more complex type bounds.
I have not seriously explored the likely edge cases which may arise in alternate syntax, and it may be that there is not a perfect design for a left-to-right-readable syntax for associated type projections. That said, it seems plausible to me that there could be at least a majority-case-covering syntax.
The following is an idea of one possible shape of that syntax, proposed only to show what I mean by "left-to-right readable." I have no particular preference for any specific symbols.
// The above code, rewritten to use some imagined shorter
// syntax for associated type projections.
THandler/IntoProtocolsHandler
::Handler/ProtocolsHandler
::OutboundProtocol/OutboundUpgrade<SubstreamRef<Arc<TMuxer>>>
::Error: Send
Happy to hear if I've missed prior art here, prior discussions, or any thoughts people may have.