impl-trait-goals icon indicating copy to clipboard operation
impl-trait-goals copied to clipboard

Conditional Bounds

Open cramertj opened this issue 7 years ago • 2 comments

The ability to specify that an impl Trait return type only implements a trait if some other bound is met.

trait Foo {
   fn foo<T>(x: T) -> impl Debug + (Clone iff T: Clone);
}

As @kimundi observed in the original impl Trait proposal, this common pattern can be seen in Rust's Iterator trait. Methods like chain return a type that is only Clone if Self: Clone and Other: Clone.

There's some discussion of this in the expanded impl Trait RFC, but I haven't seen any fully-fledged proposals for this yet.

cramertj avatar Jun 05 '17 07:06 cramertj

This is sort of an orthogonal feature, in that would be implemented for arbitrary bounds, for example, one could imagine:

fn foo<T>() where T: Foo, (T::Assoc: Clone => T: Clone)

Why have you marked this essential? To me it doesn't seem as high priority as #3 for example. Interested in hearing your reasoning!

withoutboats avatar Jun 09 '17 23:06 withoutboats

@withoutboats The only issues I've marked as essential are this issue and the one for "fully-explicit syntax". My reasoning was that all the other issues (e.g. #3) don't add anything to the overall expressiveness of the language, and can be mostly thought as sugar for the fully-explicit syntax (#1).

That said, @aturon and I spoke and agreed that #3 is very high-priority. I'll put together a PR against this repo for a pre-draft RFC so we can discuss in more detail.

cramertj avatar Jun 12 '17 01:06 cramertj