csharplang/meetings/2017/LDM-2017-10-11.md

135 lines
5 KiB
Markdown
Raw Normal View History

2017-12-06 02:06:17 +01:00
# C# Language Design Notes for Oct 11, 2017
## Agenda
We looked at the Oct 4 design review feedback for nullable reference types, and considered how to react to it.
2017-12-06 02:06:17 +01:00
1. Philosophy
2. Switches
3. Dotted names
4. Type narrowing
5. Dammit operator type narrowing
6. Dammit operator stickiness
7. Array covariance
8. Null warnings
2017-12-06 02:06:17 +01:00
# Philosophy
2017-12-06 02:06:17 +01:00
Feedback: We should view this as a linting tool. We have to make most existing code pass muster, and it's ok with a little more complexity and forgiveness in the rules to achieve that.
2017-12-06 02:06:17 +01:00
## Conclusion
We agree with this general philosophy, and it is helpful to apply it to specific decisions.
2017-12-06 02:06:17 +01:00
# Switches
Feedback: Don't have many switches and levers. Just "on" or "off". "Off" would suppress the warnings, but the `?` syntax would still be allowed. Make the hard decisions once and for all at the language level, rather than leave people with too many options.
2017-12-06 02:06:17 +01:00
## Conclusion
This is a good philosophy. We do think that there's possibly room for an "Xtreme" mode as well, for people that care more about catching more cases regardless of inconvenience.
2017-12-06 02:06:17 +01:00
# Dotted names
Feedback: We should track dotted names, and be very forgiving about what invalidates the null state. Otherwise it violates the general philosophy and complains about too much existing code. Things that *would* invalidate non-null-ness of a dotted chain is assigning to (or maybe passing to an out or ref parameter) the variable itself or any mutable prefix.
2017-12-06 02:06:17 +01:00
## Conclusion
Agree! If we adopt an Xtreme mode, this is probably one of the places where it would be harsher.
2017-12-06 02:06:17 +01:00
# Type narrowing
Feedback: when a variable of a nullable reference type (e.g. `string?`) is known to not be null, we should consider its value to be of the narrower type `string`.
2017-12-06 02:06:17 +01:00
```
void M(string? n)
{
if (n == null) return;
var s = n; // s is string, not string?
var l = s.Length; // ok
n = null; // ok
s = null; // warning
2017-12-06 02:06:17 +01:00
}
```
We previously abandoned this approach, because it doesn't work for e.g. type parameters, where we don't know if they are nullable reference types or not, and don't necessarily have an underlying non-nullable type to narrow *to*.
2017-12-06 02:06:17 +01:00
## Conclusion
This is one of those places where we should forego simplicity for friendliness. We should adopt a hybrid approach: When a type is a known nullable reference type, then having a non-null state *should* narrow its type. When it is a type parameter that might be instantiated with nullable reference types, then we can't narrow it, and should just keep track of its null state.
2017-12-06 02:06:17 +01:00
# Dammit operator type narrowing
Feedback: The dammit operator should also narrow the type of a nullable reference to be nonnullable.
2017-12-06 02:06:17 +01:00
``` c#
void M(string? n)
{
var s = n!; // s is string, not string?
var l = s.Length; // ok
}
2017-12-06 02:06:17 +01:00
T[] MakeArray<T>(T v)
{
return new T[] { v };
}
void M(string? n)
{
var a = MakeArray(n!); // a is string[], not string?[]
var l = a[0].Length; // ok
2017-12-06 02:06:17 +01:00
}
```
## Conclusion
`! should keep its warning suppression, but also narrow the outermost type when it can, to match the new behavior when null-state is non-null.
There's hesitation because of the muddiness of using `!` for two different things. But we don't have a better idea. Explicit casts are quite verbose. We may need to revisit later, but for now, `!` suppresses warnings *and* de-nullifies the type when it can.
2017-12-06 02:06:17 +01:00
# Dammit operator stickiness
Feedback: The dammit operator lacks "stickiness" - you have to keep applying it (or introduce another local).
2017-12-06 02:06:17 +01:00
One idea is to maybe have some top-level "assertion" that would declare a thing not-null for the whole scope.
2017-12-06 02:06:17 +01:00
Another idea is to have `s!` influence null state for flow analysis, staying "valid" for as long as a non-null state would have.
2017-12-06 02:06:17 +01:00
``` c#
string? s = ...
if (s != null && s.Length == 5) ... // It flows here
M(s!, s); // why not here?
2017-12-06 02:06:17 +01:00
```
It would sort of make `s!` mean the same as `s = s!`.
There are also cases where you *wouldn't* want it to be sticky, e.g. when you are using `!` to shut up a specific unannotated API that is lacking a `?`. Here you don't use `!` in the meaning of "this is really not null", but to the effect of "I am actually fine passing a null here". That meaning shouldn't really be contagious to subsequent lines.
2017-12-06 02:06:17 +01:00
## Conclusion
We don't know what, if anything, to do here. For now we'll leave it as is.
2017-12-06 02:06:17 +01:00
# Array covariance
Feedback: Arrays are (unsafely) covariant over reference types, and for consistency we should also make them covariant over nullability.
2017-12-06 02:06:17 +01:00
## Conclusion
This is probably right. We'll think about this more, but let's go with covariance for now.
2017-12-06 02:06:17 +01:00
# Null warnings
2018-02-26 21:39:07 +01:00
Feedback: Fine to warn in most places where a null value is given a nonnullable type, but we should beware of a "sea of warnings" effect. Specifically, we shouldn't warn on array creation, as in `new string[10]`, even though it creates a whole array of undesired nulls, because it is so common in current code, and couldn't have been done "safely" before.
## Conclusion
2017-12-06 02:06:17 +01:00
We agree. Xtreme mode, if we adopt that, may warn on array creation, though.