-
Notifications
You must be signed in to change notification settings - Fork 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
Discussion: tuple projection initializers #370
Comments
/cc @jcouv |
Yes. I haven't been pleased with the number of times I've had to write |
The use case comes up almost immediately var indexedAndFiltered = elements
.Select((element, index) => (element, index))
.Where(x => x.index % 2 == 0) // error
.Select(x => x.element); // error The above should work. Currently, using a tuple results in code that is more performant and easier to write than using an anonymous type but is less readable. That should not be the case and it does not need to be here. |
@alrz I like the idea (inferring names for tuple elements based on the expressions used), and @aluanhaddad's motivating example. More supporting examples would be nice. If I understand your proposal, we would:
Digging into the first part, some examples of expressions:
Although I don't see much of a problem with it, I want to point out that there would be no way of overriding this. You could choose another name ( Another important consideration is that when you use |
As for extracting names from the expression, I'm thinking that we should use the existing
Not just duplication, ambigious names should be dropped as well, e.g.
The first example demonstrates how we can override names. If we do not use those names, I don't see any reason to be able to explicitly "unname" an element (is there a case where this is an issue?), however, the inferred name shall not contribute to "name mismatch" warnings and such. |
@alrz Thanks for the pointer on I took a quick stab to see what this would look like. I'll bubble the idea around to see how it ranks. |
As has been noted elsewhere, this would be a breaking change. Doesn't mean it can't happen. |
This demonstrates the breaking change. static void M<T>(this (T, Action M) t) => (1, t.M).M(); What are the odds. |
From discussion with LDM and compat council today, we will pursue this change in C# 7.1. |
Promoted to a championed issue #415. Next step: I'll capture our notes in a proposal/spec and finish the implementation (in PR dotnet/roslyn#18374) |
static void M<T>(this (T, Action M) t) => (1, t.M).M(); @alrz |
Any chance of getting this in C# too? |
@Grauenwolf this will be available in C# 7.1 |
@Grauenwolf This may be worth starting a new discussion on. It would be good to look at the cases supported by VB and consider each difference. |
@alrz Grauenwolf is asking about inferring names from invocation expressions, in C# as VB does. That's not in C# 7.1. |
@jcouv As I see it, the change is now or never. We're breaking compatibility between 7.0 and 7.1 anyways, but I don't think that we'd be able to a second time. |
It's definitely a nice to have, both for tuples and anonymous types, but I wouldn't cry if I didn't get it. I would consider it a bit of polish, just like tuple name inference in general. |
This is not a breaking change, as per dotnet/roslyn#13319.
The text was updated successfully, but these errors were encountered: