Skip to content
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: Splatting #8987

Closed
alrz opened this issue Feb 21, 2016 · 6 comments
Closed

Proposal: Splatting #8987

alrz opened this issue Feb 21, 2016 · 6 comments

Comments

@alrz
Copy link
Contributor

alrz commented Feb 21, 2016

As it is proposed in #347 you might be able to use splatting in method arguments

public double Avg(int sum, int count) => count == 0 ? 0 : sum / count;
Console.WriteLine($"Avg: {Avg(Tally(myValues))}");

And unsplatting would be like:

var list = List<(string name, int age)>();
list.Add("John Doe", 66); 

In the first case it's rather too implicit and as discussed, there should be some kind of operator to make it more visible.

Console.WriteLine($"Avg: {Avg(...Tally(myValues))}");

I want to propose the ability to use this syntax on tuples themselves e.g.

var foo = (1,2);
var bar = (...foo, true);

// translates to
var bar = (foo.Item1, foo.Item2, true);

In this case the target expression must be of a tuple type (names will be preserved in the resultant tuple).

Same syntax can be used with anonymous types.

var foo = new { Name = "foo", Family = "bar" };
var bar = new { ...foo, Age = 0.5 };

// translates to
var bar = new { foo.Name, foo.Family, Age = 0.5 };

This is specifically useful in pipeline processing (like LINQ) to compose short-lived tuples with additional information added in each step.

@DavidArno
Copy link

I'd suggest the syntax for your first two examples would probably need to be:

public double Avg(int sum, int count) => count == 0 ? 0 : sum / count;
Console.WriteLine($"Avg: {Avg(...Tally(myValues))}"); // ... means map tuple to two params of Avg

And

    var list = List<(string name, int age)>();
    list.Add(("John Doe", 66)); // inner () needed to denote it's a tuple, not two separate params 

@alrz
Copy link
Contributor Author

alrz commented Feb 22, 2016

@DavidArno That's the exact syntax proposed in #347. The rest of examples are my suggestions.

@DavidArno
Copy link

@alrz,

Thanks. I've cross-referenced my suggestion there too.

@bondsbw
Copy link

bondsbw commented Feb 22, 2016

If ... is used as an actual language syntax, it could completely change the meaning of many of the issues and proposals on in this forum. 😉

@alrz
Copy link
Contributor Author

alrz commented Feb 22, 2016

@bondsbw I'm not fond of the syntax here, however, this is what ES, C++ and Java are actually using for various purposes. "it could completely change the meaning of many of the issues and proposals on in this forum" yeah actually it's kind of a breaking change ;).

@alrz
Copy link
Contributor Author

alrz commented Apr 10, 2017

Moved to dotnet/csharplang#424

@alrz alrz closed this as completed Apr 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants