Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upIdea: default types for traits #2211
Comments
This comment has been minimized.
This comment has been minimized.
|
My only thought on the matter:
This property is far from unique. Even in |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Nov 7, 2017
|
If you're in |
This comment has been minimized.
This comment has been minimized.
ExpHP
commented
Nov 7, 2017
•
|
I wish this were a property of the functions rather than the known trait bounds. I feel like a default of Type parameter defaults for functions were accidentally supported in the grammar for some time and I'm sure I've written them a number of times expecting them to do something. I gather from that issue that it is not necessarily clear how these should behave, but without being able to find much discussion on it I'm not sure I see how they are any muddier than defaults for parameters in types and traits. I guess the difference for structs and traits is that those defaults are applied each and every time we write out the type, regardless of context. Whereas in the case of functions, we're moreso hoping for the default to merely act as a "suggestion" to type inference. |
Centril
added
the
T-lang
label
Dec 6, 2017
This comment has been minimized.
This comment has been minimized.
leodasvacas
commented
Feb 28, 2018
This comment has been minimized.
This comment has been minimized.
|
One place this could be useful is in automatic-enuming of return types in futures. For example: say I have .and_then(|x| {
match x {
true => some_future,
false => some_other_future,
}
})This won't compile because .and_then(|x| {
match x {
true => Box::new(some_future) as Box<Future<...>>,
false => Box::new(some_other_future) as Box<Future<...>>,
}
})However this does an unnecessary allocation. What we really want is to create an anonymous 2-variant enum that implements |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Mar 5, 2018
|
If you're already doing a |
This comment has been minimized.
This comment has been minimized.
|
@burdges I guess so, yeah Would still be nice to have generic-arity anonymous enums for this though. |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Mar 16, 2018
|
We could maybe unify the various |
canndrew commentedNov 7, 2017
Some types are only visible through the traits that they implement. Currently, when Rust cannot infer a type it will either raise an error or, in some unclearly-defined situations, default the type to
!(or()). Perhaps we could allow defaulting in more situations and allow the defaulting to be trait-directed.For example, the signature of
Iterator::unzipis:Now suppose I write:
If I haven't touched
fooanywhere else my code, then this won't compile. All rust knows aboutfoo's type is that it must implDefault + Extend<X> + IntoIterator<Item=X>, and there may be lots of types that satisfy this requirement. HoweverVec<X>is, in some sense, the canonical type that satisfies these requirements - when iterated it gives back the exact same items that where.extend-ed into it, in the exact same order. Perhaps the compiler should be able to inferVec<X>in this case. Perhaps the standard library should be able to specifyVec<X>as the inferred type in cases like this with a declaration like:Multiple
defaultdeclarations could override each other by being more specific in much the same way that impl specialization works. When the compiler tries to default a type which satisfies some required boundsB, it looks for the most specific traitA <: Bfor which there is a default type, and uses that type if it satisfiesB.The current behaviour of defaulting to
!could be replaced by a declaration inlibcoreof:There could also be default of (just for example):
So, is this a good idea? Or is it terrible?