meetings/2016/LDM-2016-10-25-26.md
In C# 6.0 we embraced, but eventually abandoned, a very general notion of "declaration expressions" - the idea that a variable could be introduced as an expression int x.
The full generality of that proposal had some problems:
int x is unassigned and therefore unusable in most contexts. It also leads to syntactic ambiguitiesint x = e, so that it could be used elsewhere. But that lead to weirdness around when = e meant assignment (the result of which is a value) and when it meant initialization (the result of which is a variable that can be assigned to or passed by ref).However, there's value in looking at C#'s new deconstruction and out variable features through the lens of declaration expressions.
Currently we have
(x, y) = e(X x, Y y) = eM(out X x)This calls for generalization. What if we said that
(e1, e2) can be lvalues if all their elements are lvalues, and will cause deconstruction when assigned toX x that can only occur in positions where an unassigned variable is allowed, that is
Then all the above features - and more - can be expressed in terms of combinations of the two:
Given the short time left for fixes to C# 7.0 we could still keep it to these three special cases for now. However, if those restrictions were later to be lifted, it would lead to a number of other things being expressible:
(x, int y) = eM(out (x, y))int x = eThe work involved in moving to this model without adding this functionality would be to modify the representation in the Roslyn API, so that it can be naturally generalized later.
Let's re-cast the currently planned C# 7.0 features in turns of declaration expressions and tuple expressions, and then plan to later add some or all of the additional expressiveness this enables.
Some patterns are "irrefutable", meaning that they are known by the compiler to be always fulfilled. We currently make very limited use of this property in the "subsumption" analysis of switch cases. But we could use it elsewhere:
if (GetInt() is int i) { ... }
UseInt(i); // Currently an error. We could know that i is definitely assigned here
This might not seem to be of much value - why are you applying a pattern if it never fails? But in a future with recursive patterns, this may become more useful:
if (input is Assignment(Expression left, var right) && left == right) { ... }
... // condition failed, but left and right are still assigned
This seems harmless, and will grow more useful over time. It's a small tweak that we should do.
It's a bit irksome that deconstructors must be written with out parameters. This is to allow overloading on arity, but in practice most types would only declare one. We could at least optionally allow one of them to be defined with a return tuple instead:
(int x, int y) Deconstruct() => (X, Y);
Instead of:
void Deconstruct(out int x, out int y) => (x, y) = (X, Y);
Evidence is inconclusive as to which is more efficient: it depends on circumstances.
Not worth it.
With the new scope rules, folks have been running into this:
if (int.TryParse(s1, out var i)) { ... i ... }
if (int.TryParse(s2, out var j)) { ... i ... } // copy/paste bug - still reading i instead of j
It works the same as when people had to declare their own variables outside the if, but it seems a bit of a shame that we can't do better now. Could we do something with definite assignment of out variables that could prevent this situation?
Whatever we could do here would be too specific for a language solution. It would be a great idea for a Roslyn analyzer, which can
TryFoo methods by looking for "Try" in the name and the bool return typeCan we make _ the wildcard instead of *? We'd need to be sneaky in order to preserve current semantics (_ is a valid identifier) while allowing it as a wildcard in new kinds of code.
M(out var _);
int _ = e.M(_ => ..._...x...); // outer _ is still a named variable?
(int x, var _) = e.M(_ => ..._...x...);
(_, _) = ("x", 2);
We would need to consider rules along the following lines:
_ always a wildcard in deconstructions and patterns_ always a wildcard in declaration expressions and patterns_ as a wildcard in other l-value situations (out arguments at least, but maybe assignment) when it's not already defined_ to be declared more than once in the same scope, in which case it is a wildcard when mentioned_ to be "redeclared" in an inner scope, and then it's a wildcard in the inner scope"Once a wildcard, always a wildcard!"
This is worth pursuing. More thinking is needed!