When destructuring a struct, it’s pretty annoying to have to write out the name of the struct. It usually leads to redundancy as the type of the struct is knowable from context. An example:
struct Point<T> {
x: T,
y: T,
}
// `Point` is repeated in the function argument destructure
fn pt_to_tup_1<T>(Point { x, y }: Point<T>) -> (T, T) {
(x, y)
}
fn pt_to_tup_2<T>(point: Point<T>) -> (T, T) {
// `Point` is repeated in the destructure
let Point { x, y } = point;
(x, y)
}
In both cases Point
is unnecessarily (except that the compiler requires it) specified on the LHS even though it's inferable from the RHS; the only thing something of type Point<T>
can be destructured into is another Point
(right?). Having to name the type in order to destructure it gets particularly annoying when the type has a long name or needs qualification due to not yet being in scope.
I'd like to be able to write the following but this is currently not allowed.
fn pt_to_tup_3<T>(_ { x, y }: Point<T>) -> (T, T) {
(x, y)
}
fn pt_to_tup_4<T>(point: Point<T>) -> (T, T) {
let _ { x, y } = point;
(x, y)
}
- Are there any plans to add this to the language?
- If not, what's the way to go about proposing this?
- Is this the right forum to ask such questions and make language related suggestions?