Roslyn: [Feedback] Official change of mind: deconstruction is actually a really great new feature

Created on 13 Jan 2017  路  7Comments  路  Source: dotnet/roslyn

Still not sure if this is the best place to post this feedback, but I'll give this a go.

Some time ago (I can't find the precise issue), I argued that Deconstruct was a nasty feature that I really wouldn't see myself using. Now fast forward to a couple of weeks ago when I was converting my Succinc<T> library to C# 7 and my views on Deconstruct did a massive about-turn.

The library contains support for the FP-feature, cons. It provides this for IEnumerable<T> by wrapping such enumerations in ConsEnumerable<T>, which caches the enumeration as it's read, thus allowing items to be added/removed from the head, re-read etc, all without iterating over the enumeration more than once. So for the following F#-like code:
fsharp let head :: tail = someList
The equivalent in C# becomes:
cs var consResult = someList.Cons(); var head = consResult.Head; var tail = consResult.Tail;
Which isn't great syntax; but it worked.

However, with deconstruction support in C# 7, I can add a Deconstruct extension method directly to IEnumerable<T>, and suddenly the code becomes:
cs var (head, tail) = someList;

Even better, as deconstruction is recursive in nature, it becomes possible to "cons" multiple heads off the list in one go:
cs var (head1, (head2, (head3, tail))) = someList;
Which is pretty bloody awesome!

So thanks language team: you have delivered one very useful feature here. 馃榾 馃帀

Area-Language Design Discussion

Most helpful comment

The combination of tuples and deconstruction make for some interesting syntax possibilities. I'm personally a fan of using them for asynchronous join operations:

var (result1, result2) = await (DoSomethingAsync1(), DoSomethingAsync2());

All 7 comments

@DavidArno The more "real-world" examples of new feature(s) usage the better.

The combination of tuples and deconstruction make for some interesting syntax possibilities. I'm personally a fan of using them for asynchronous join operations:

var (result1, result2) = await (DoSomethingAsync1(), DoSomethingAsync2());

Even better could be implicit deconstruction into function parameters

Print2D ((1,1)); // Print2D require two parameters

@vbcodec that's construction of a tuple

@jnm2

... and passing it to function that require two arguments

@vbcodec That won't work without tuple splatting which is not currently available but it is being considered as stated in the original thread (#347).

@gafter, seriously, this isn't a discussion. It's a "thank you" to pass on to the team and to then close when you are ready. Because I do actually like the language team, honest 鉂わ笍

Was this page helpful?
0 / 5 - 0 ratings