C# Design Notes for Nov 15, 2016 #16674
Labels
Area-Language Design
Design Notes
Feature - Out Variable Declaration
Out Variable Declaration
Feature - Pattern Matching
Pattern Matching
Feature - Tuples
Tuples
Language-C#
Language-VB
C# Language Design Notes, Nov 15, 2016
Agenda
Tuple name warnings
There are two kinds of warnings on the table regarding names in tuples:
The first warning helps get the names right when writing a tuple literal, and while maintaining it. It is already in the product:
The second warning would help guard against bugs due to reordering of tuple elements:
This warning is currently not in the product. We would like it to be, but don't have the runway to implement it.
In the future it could be added as an analyzer, or as a compiler warning protected by warning waves (#1580).
"Discards"
We got great feedback on "wildcards" at the MVP Summit, including changing its name to the term "discards", which is spreading in industry.
We are also encouraged to use
_
as the discard character instead of*
. It works better visually, and is what many other languages already use.There is a small matter of
_
already being a valid identifier. In order for it to coexist as a discard with existing valid uses we need to use a few tricks. Here are the essential rules:_
when no_
is defined in scope is a discardvar _
orT _
in deconstruction, pattern matching and out vars is a discardDiscards are like unassigned variables, and do not have a value. They can only occur in contexts where they are assigned to.
Examples:
One use case is to silence warnings when dropping Tasks from async methods:
We also want to allow discards as lambda parameters
(_, _) => 0
, but don't expect that to make it in C# 7.0. Today_
is allowed as an ordinary identifier; the rule would be that it would become a discard if there's more than one.We can also consider allowing top-level discards in foreach loops:
But that does not seem too important, especially since you can use
var _
to the same effect.In general, whenever use of standalone
_
as a discard is precluded, either by syntactic restrictions or by_
being declared in the scope,var _
is often a fine substitute with the same meaning. However, if the declared_
is a parameter or local, there are situations where you are out of luck:These situations are always local to a member, so it is relatively easy to rewrite them, e.g. to rename the incoming parameter. We could consider accommodating them in the future: the error for a local redeclaration of
_
when one is already in scope could be changed to allowing it, but consider the second declaration a discard. This may be too subtle, and not useful enough, but it is worth considering post C# 7.0.The text was updated successfully, but these errors were encountered: