-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Figure out a way to ergonomically match against impl traits? #2261
Comments
Perhaps we could use a |
Perhaps a first step might be the ability to name the concrete type of a function that returns |
Right now, coproduct types are called There are separate discussion around both |
I don't want to derail the issue with this side-discussion... But that is simply not true. We wouldn't necessarily need anonymous sum types... you might be able to do it with: impl<A, B> Future for Coproduct<A, B> // Coherence troubles today. so #[fundamental]
where
A: Future,
B: Future<Item = A::Item, Error = A::Error>
{
type Item = A::Item;
type Error = A::Error;
fn poll(&mut self) -> Poll<A::Item, A::Error> {
match *self {
Coproduct::Inl(ref mut a) => a.poll(),
Coproduct::Inr(ref mut b) => b.poll(),
}
}
} let f = async_entry_point().or_else(move |err| match err {
ErrorCode::Case1 => case1().inject(), // Explicitly inject(), you can add 100 more variants if you like.
ErrorCode::Case2 => case2().inject(),
}); |
futures-rs has an |
Closing in favor of #2414. |
impl trait
as currently implemented with anonymized types is more of syntactic sugar than anything else, the inability to match against functions returning the sameimpl trait
is.. unfortunate. This becomes painfully obvious when trying to work withfutures-rs
, which expresses the concept of a "future" as a trait but each future transform results in a distinct type that is not compatible from other future types.For example, there (imho) needs to be a way for this code to compile without inane/insane workarounds:
(source code available at https://git.neosmart.net/mqudsi/futuretest/src/branch/rfcs-2261)
which currently does not compile because while the two functions return an object implementing the same trait, the compiler generates two wholly distinct anonymized types, one for each function:
I can think of one very simple solution to this, but I can't write it out due to the anonymized types: for a match block where all the entries are anonymized types implementing type X or else standard types implementing type X, create a new, anonymized enum type E with a case for each, convert each anonymized type implementing X to
E::{X1,X2,X3,..,Xn}
, and then decompose the value at the point where the result bubbles up to a comparison that must be fully typed to succeed.In retrospect, this is actually not specific to
impl trait
any more, can be used to match against n types sharing one or more traits.The text was updated successfully, but these errors were encountered: