3 ms·
This might be the most exciting group of new features since the beginning of C#. Those tuples...seriously sexy. But, as much as I love C# (I changed jobs so I
by redleggedfrog 10y ago
This might be the most exciting group of new features since the beginning of C#. Those tuples...seriously sexy.
But, as much as I love C# (I changed jobs so I could work in it) this just has a pungent code smell to me:
p.GetCoordinates(out var x, out _); // I only care about x
Ug, some random underscore? I'm no language designer, but something maybe could have been done better here? It just looks fugly to me.
- johncolanduoni 10y agoIt's pretty common in languages with pattern matching to use underscore as a catch-all (ML family, Haskell, etc.). I'm not sure how the underscore is a code smell; if you want wildcard patterns without throwing a bunch of unused variables into scope (which can definitely lead to non-obvious bugs), you have to use some symbol.
- redleggedfrog 10y agoMostly like if I submitted some code for review and peppered it with underscores because "I don't care about those variables" I'm pretty sure it'd come back to me with a remark about starting to care. At the point your writing code you don't care about, something might be wrong.
- johncolanduoni 10y agoDo you ever write an if statement where you don't check one of the fields of an object, or a method call where you ignore a return value? If so, how is this any different?
- bschwindHN 10y agoWhen working with an object, you don't always care about all of its properties. If it's packed in a certain way (tuple, Optional wrapper, etc.) that is easy to unpack with destructuring, then you might use an underscore to say "this is how it's structured, but I don't care about the particular value it has so please don't bind it to a variable." If you bound it to a variable and didn't use it, it'd be the same as declaring an unused variable.
- bruxis 10y agoYeah, why not "out null" or something similar? Magic characters make me sad.
- rjblackman 10y agoUnfortunately you have to download a nuget package to get the tuple syntax to work..