-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Deconstruction-assignment should return a tuple type #12635
Comments
Out of curiosity what is the reason for the change? I'm not questioning it as much as I'd like to understand it. I gather that having positional deconstruction return a tuple is easier to write particularly given that you can resort to simple expressions in the method. However they would also be a bit slower given the need to copy the resultant tuple back to the caller (and then potentially deconstruct again). Can the community hope to see more LDM notes? It's been several months since the last drop and I think several members are getting a bit antsy here. |
Why not have the compiler simply recognize properties with the names "Item1", "Item2" etc? Wouldn't that offer the best possible performance, since accessing those properties would be inlined anyway, and properties going into wildcards (*) would not even need to be accessed? |
They could only be inlined if they were relatively simple, which the might be for types that are effectively tuples but could be more complicated if the individual members would need to be calculated. Being proper properties would pollute the members of existing types regardless of whether or not they're used as tuples. If properties were considered I'd rather attributes that would decorate existing properties which would specify the positional order. However, the use of properties at all shuts down the ability to define extension members to perform deconstruction, since "extension everything" isn't slated for C# 7.0. I think using tuples works pretty well here. It aids in the case of the deconstruct method using (eventually) pattern matching expressions to construct the result. One thing that a tuple return type does mess up is that you'd lose the ability to overload deconstruction by the arity or count of the tuple elements. |
@andrew-skybound @HaloFour Regular assignment expressions have a value that you can continue with. For example: So if you do |
To clarify, the discussion of having the assignment return a tuple does not mean the deconstruction would change. We still use a |
So this is an implementation detail? What would the public void Deconstruct(out int x, out int y) {
x = 123;
y = 456;
}
public (int x, int y) Deconstruct() {
return (123, 456);
} |
@HaloFour public void Deconstruct(out int x, out int y) {
x = 123;
y = 456;
} The benefits of this design is that you can introduce more parts over time (Deconstruct with 3 or 4 out parameters) and you can implement Deconstruct as an extension method. |
Properties for the most common case of tuple-like types (Tuple, KeyValuePair, Point, Rectangle, Vector, etc) are all guaranteed to be inlined, regardless of the tuple arity, meaning that deconstructing one of these instances will be as cheap as individual assignments. A
[EditorBrowsable(EditorBrowsableState.Never)]
Using attributes would make it impossible to extend types which cannot be altered.
The compiler could support Sorry, I misinterpreted the topic to be referring to the return type of the
I am worried about using |
Per LDM 7/13, if we have time, let's have the return type be a tuple type, instead of void.
The tuple should only be formed if there is code to consume it.
The text was updated successfully, but these errors were encountered: