Description
openedon Apr 23, 2018
First, the idea was lain down by @glaebhoerl here.
The idea is basically to have a way to tell the compiler “please auto-generate me a sum trait for my -> impl Trait
” function, that would automatically derive Trait
based on the implementations of the individual members.
There would, I think, be a need for a syntax specially dedicated to this, so that people are not auto-generating these without being aware of it. Currently, the two syntaxes I can think of are either |value|
(without a following {}
), running on the idea of “this is auto-generated, just like closures” (I don't like it much, but it could make sense), and the other idea is to make it use a keyword. I'd have gone with auto
, but it appears to not be reserved, and become
or enum
would likely be the best choices.
(Edit: @Pauan pointed out that the |…|
syntax would be ambiguous in certain cases, so please disregard it)
So the syntax would, I think, look like this:
fn foo(x: bool) -> impl Iterator<Item = u8> {
if x { return become b"foo".iter().cloned() }
become b"hello".iter().map(|c| c + 1)
}
// Or:
fn foo(x: bool) -> impl Iterator<Item = u8> {
if x { return enum b"foo".iter().cloned() }
enum b"hello".iter().map(|c| c + 1)
}
// Or:
fn foo(x: bool) -> impl Iterator<Item = u8> {
if x { return |b"foo".iter().cloned()| }
|b"hello".iter().map(|c| c + 1)|
}
The major advantage of the ||
syntax is that it doesn't raise the issue of parenthesizing, as it's already made of parenthesis.
What do you think about this idea, especially now that impl Trait
is landing and the need is getting stronger?