-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement From<L> and From<R> for Either<L, R> #33
Comments
I don't think the compiler will allow this. What if |
That's a good point. Is there no way to make this work? Some way to only implement this only in the case that |
There's currently no way to specify Then there's the problem that adding a blanket |
Doesn't look like that rfc will be merged soon or at all, but yeah it looks to be what's required. However, I didn't notice that I could simply impl this myself, so I might just do that. Thanks for the tip! Maybe a macro in this crate like |
On the topic of the breaking change: With specialization, shouldn't this be non-breaking? Since an already existing impl should be strictly more specific than a blanket impl. |
Hmm, maybe specialization will allow this, but that's not stable yet: rust-lang/rust#31844 |
Maybe those impls could also be hidden behind a non-default feature flag? |
either is 1.0 and should avoid feature flags. I'll restart from the top, bear with me. Similar use case scenarios: 1fn foo(e: Either<i32, f32>) {
// use e
}
fn main() {
foo(32.into());
foo(42.0.into());
} 2fn foo(e: Either<i32, f32>) {
// use e
}
fn main() {
foo(Either::from(32));
foo(Either::from(42.0));
} 3fn foo<E>(input: E) where E: Into<Either<i32, f32>> {
let e = input.into();
// use e
}
fn main() {
foo(32);
foo(42.0);
} My thoughts(2) is a nicer interface than (1), and (3) is also a very nice API. The reason that much prefer 2 and 3 over 1, is that A, the trailing (2) is nice because its no longer anonymous, it's half-explicit about the type conversion (with option to make it entirely explicit). And (3) is another nice alternative to (1) which is usually less type inference sensitive, in part because the API side (the person providing I also dislike (1) also makes it hard to transition to a (3)-style API if you want to add more custom conversions or make it more generic for other reasons. Summary: prefer to design to end up with (2) or (3) |
Thanks for the extensive comment! It looks like all 3 of these would be enabled by having a |
What's the status on this feature? Is there still an issue if |
From my perspective, this feature will never be possible. L and R being equal is always going to be an issue. I think we can close it, that's the least frustrating thing we can do, instead of holding it open as if it's a possibility. Thanks |
I suggest adding the two impls:
and
(don't know the right syntax by heart)
This enables the following nice use-case:
Instead of explicitly writing out the type each time.
Is there a problem with this/am I missing something? Otherwise I would write up a PR. Thanks!
The text was updated successfully, but these errors were encountered: