-
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
Proposal: Projection initializer for tuples #13319
Comments
I'm confused what you are asking for here, as the following works just fine: var x =
from item in list
select (item.Name, item.Age); |
He's asking for a way to have the tuple elements automatically named based on the properties used to initialize them, as with anonymous types. Your code example does work but the tuples go unnamed. var x =
from item in list
select (item.Name, item.Age);
for (var y in x) {
Console.WriteLine($"Hello {y.Name}!"); // oops, you'd need to use y.Item1
} I'm curious as to why the compiler couldn't just automatically use the same rules as it does with anonymous types in this context? Does it introduce any ambiguity? |
In that case, the second piece of code compiles and works just fine (using VS15 Preview 4): var x =
from item in list
select (Name: item.Name, Age: item.Age); So I'm still confused 😄 |
That it does. That's why he states that he wants the first example to be equivalent to the second example. He doesn't want to have to explicitly provide the tuple names where they could be automatically deduced. |
Ah, got it now. Thanks for taking the time to explain. In thicky-mode today 😄 |
@HaloFour I don't recall the issue number (design notes) but this idea was rejected because tuple item names are not mandatory compared to anonymous types where they are and there must not be any duplication. However, since names are insignificant in type equivalency I don't really think that is a blocker. |
I haven't been able to find any design notes on the subject either. If anything I think that since the names are not mandatory that there's less reason to not implicitly name the elements. It's not like doing so would break accessing them by their underlying tuple property name. It's my opinion that a common use case for tuples would be projections just as you demonstrated, and either having to either explicitly specify the name or reference the element by position will both be considered obnoxious (certainly not DRY). Seems like a simple win. |
Now I understand this proposal (thanks @HaloFour), I'm in complete agreement with it and think it a sensible idea. For the first code example: var x =
from item in list
select new ValueTuple<string, int> { item.Name, item.Age }; The complier is inferring the types of the tuple and can infer the names too. So it should. Calling them It should be limited to such simple scenarios though, eg if the code were: var x =
from item in list
select new ValueTuple<string, int> { item.Name, item.Age * 2 }; Then in this case, the second value is not |
@DavidArno That is nothing new, you can refer to end of the 7.6.10.6 section in C# spec. The problem is that this can be intuitively integrated to the regular tuple literals, so that |
@HaloFour After digging through design notes I finally found the relevant part,
I'll note that name inference makes the bizarre /cc @MadsTorgersen |
This is a good idea. If there is a meaningful name that can be inferred and served up to consumers, especially given the precedent set by anonymous types, there is lot to be gained from this. The difference that anonymous types require names be specified where names cannot be inferred but that tuples work in the opposite manner is not a reason not to do this. This is a tooling win. |
@jcouv Any news on this? I don't think that it would be a breaking change to add this later though. |
@alrz I agree. I don't think this would be a breaking change to add later either. That makes it less of a priority to decide now compared to other issues we're currently working through (discards, scoping rules for out and pattern variables, tuple errors that can't be introduced later, ...). So this won't be in C#7.0. After that, it will be good to see more real-world usage with tuples and deconstruction. This experience and feedback will help prioritize and trade-off features. |
Since tuples won't infer names from the expression [1], would it make sense to be able to use a property initializer with tuples to retain similar behavior?
would be equivalent to:
Using #35 one should be able to omit the type so that
new () { .. }
defaults to a tuple:Alternatively we could use a "struct anonymous type",
This could generate an internal
struct
or just use a tuple under the hood.[1] This was mentioned in #10429 but if anything, the original idea (using regular tuple syntax) would be a better solution in any ways.
The text was updated successfully, but these errors were encountered: