A shorthand for implementing traits

This has previously been discussed; I specifically recall this thread, though I also think @Soni has mentioned it more recently. (Side note: linking is for context purposes only, and not endorsement of any specific author or post in linked thread. (Note: this disclaimer is not trying to put down any author or post in the linked thread.))

While the amount of redundant typing (note: typing as in types in the language, not keyboard typing) reduction is limited in the simple case, more interesting bounds on generic types can lead to a lot of duplicated type information for multiple trait impls.

Additionally, impl Type { impl Trait { fn foo() {} } } is potentially useful as an inherent impl, where the Trait functions are available for Type even without bringing Trait into scope, equivalently to if extra forwarding inherent impls were provided. (Including precluding a separate inherent impl with the same name.)

Implied bounds for inherent trait bounds would reduce again the benefit of deduplicating the typing overhead, but there are still cases where the ideal situation of bound-free type definitions and bounded impls are useful and would reduce typing overhead there.

The main downside I can see, though, is that the deduplicating would encourage over-bounded trait impls, as it'd be easier to provide one superset bounded impl and impl all traits inside it, than implement the traits separately with proper bounds.

I still personally like the idea, but if and only if the functions are (all) also provided as inherent functions. This meaningfully separates it from the separate item case (which it desugars to).

2 Likes