-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
C# Design Notes for Oct 25 and 26, 2016 #16640
Comments
Something else about a wildcard |
Getting these notes from the design meetings is great but these feel so out of date compared to the conversations that have already taken place here. I know that you guys are busy but it would be nice to get these in a more timely fashion. You can do the following today: Type temp = typeof(Dictionary<,>);
Type type = temp.MakeGenericType(typeof(string), temp.GetGenericArguments()[1]); Of course it's a bit messy but that's basically what the compiler would be required to emit as I don't think that you can obtain a type handle to a partially open generic type. The big question would be what use case you intend to satisfy? Partially open generic types are nasty beasts, moreso than open generic types. The CLR has very little support for doing anything with them. There might be arguments for using open or partially open generic types in patterns, but you mentioned |
@HaloFour I don't really have a usecase, just drawing a loose syntactic parallel for specifying types with unbounded generic values. Maybe that could come into play in the future when matching on types, or inferring them. I haven't given it much thought though. |
I think the members of community have raised their concerns about this in past #12597 , #12939. Perhaps it's worth reverting the late scoping changes?
Not sure if this is the case as when something is declared outside it is a strong indication that it is intended to be used outside by the developer. |
@HaloFour is it valid syntax to fill only some generic? Such as |
Re: Tuple-returning deconstructors Re: Definite assignment for out vars However, if you won't do that, providing work-arounds via analysers is probably the best course of action. Re: Irrefutable patterns |
I wish we could return immutable reference, nullable reference and tuple reference so we could drop |
What is the actual reason for wanting this in the first place? It seems like a change that requires a lot of work and sneakiness, and what exactly is the benefit? |
@HaloFour Apologies for posting the notes late. I am catching up after a very hectic time. There are a few more in the pipeline. As you say, most of this has been communicated through other channels already - the main reason to still post them is so that the rationale and extra detail is captured. @GeirGrusom The reason for the shift to There are more details on wildcards in #16674, which I just posted - including a name change to "discards". |
LDM notes for Oct 25/26 2016 are available at https://github.com/dotnet/csharplang/blob/master/meetings/2016/LDM-2016-10-25-26.md |
C# Language Design Notes for Oct 25 and 26, 2016
Agenda
Declaration expressions
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) = e
M(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 isThen 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) = e
M(out (x, y))
int x = e
The 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.
Conclusion
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.
Irrefutable patterns
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:
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:
Conclusion
This seems harmless, and will grow more useful over time. It's a small tweak that we should do.
Tuple-returning deconstructors
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:
Instead of:
Evidence is inconclusive as to which is more efficient: it depends on circumstances.
Conclusion
Not worth it.
Definite assignment for out vars
With the new scope rules, folks have been running into this:
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?Conclusion
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 thebool
return typeUnderbar wunderbar
Can 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.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!"
Conclusion
This is worth pursuing. More thinking is needed!
The text was updated successfully, but these errors were encountered: