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: allow deconstruction in more places (let, from, lambda) #13964

Closed
jcouv opened this Issue Sep 21, 2016 · 4 comments

Comments

Projects
None yet
3 participants
@jcouv
Copy link
Member

jcouv commented Sep 21, 2016

We only were able to implement P0 deconstruction scenarios in C# 7, which leaves a number of candidates unaddressed (let clause, from clause, lambda, method declaration, using, outvar, join clause, query continuation clause, join into clause, catch).

The first three in particular could use further investigation.

From clause

Instead of:

(int, int)[] tuples = new[] { (1, 2), (3, 4) };
var query = from t in tuples
        select t.Item1;

Allow:

var query = from (x, y) in tuples
        select x;

Let clause

var query = from deconstructable in deconstructables
        let (x, y) = deconstructable
        ...;

Lambda

Instead of:

tuples.Select(t => t.Item1 + t.Item2);

Allow:

tuples.Select((var (x, y)) => x + y); 
// the lambda takes one argument that is not named 
// and can be broken into two parts

Or maybe this syntax instead:

tuples.Select(((x, y)) => x +y); // same number of parentheses, but denser

FYI @dotnet/roslyn-compiler @MadsTorgersen @CyrusNajmabadi @kuhlenh

@mburbea

This comment has been minimized.

Copy link

mburbea commented Sep 22, 2016

tuples.Select(((x, y)) => x +y);

That lambda syntax is going to confuse the heck out of readers and could lead to subtle errors if I forget a level of parens.

tuples.Select(((x, y)) => x +y);
// calls Enumerable.Select<T,TRet>(this IEnumerable<T>, Func<T,TRet>)
tuple.Select((x,y)=> x+y);
//calls Enumerable.Select<T,TRet>(this IEnumerable<T>, Func<T,int,TRet>)

And this one will trip me up because of the var.
tuples.Select((var (x, y)) => x + y);

@paulomorgado

This comment has been minimized.

Copy link

paulomorgado commented Sep 22, 2016

So, what do you propose, @mburbea?

@mburbea

This comment has been minimized.

Copy link

mburbea commented Sep 23, 2016

In something like the sugar of the from clause it looks super natural, but in the case of a simple func, it just odd to look at especially because parentheses nesting normally does not change meaning. I think case is more agreeable, but I'm comfortable without it being present in lambdas, provided I could at least get a friendly name e.g.

.Select((var x, var y) u=> u.x+ u.y) is a lot better than Item.

@jcouv

This comment has been minimized.

Copy link
Member Author

jcouv commented Apr 9, 2017

This proposal has been moved to C# lang repo:

@jcouv jcouv closed this Apr 9, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment