Closed
Description
openedon May 8, 2017
This proposal/discussion is an addendum to #36 to make official the proposal of a "damnit" operator which can be used to forcibly unwrap a nullable value into a non-nullable value.
In the current non-nullable reference proposal it would cause a warning to be emitted if a nullable reference is passed/assigned to a non-nullable reference value where flow analysis cannot guarantee that the reference is not null. In those cases the developer could use the postfix ! operator to explicitly instruct the compiler that the value would not be null in that circumstance which would suppress the warning:
string? GetName() => ...;
void PrintName(string name) => Console.WriteLine(name);
string? name = GetName();
PrintName(name); // emits warning
PrintName(name!); // suppresses warningFor the sake of symmetry I also propose that this operator be available on nullable value types. It would be equivalent to calling the Value property:
int? length = name?.Length;
int x = length!; // equivalent to length.Value or (int)lengthOpen Questions:
- Should the
!operator cause an immediate exception if the value in question isnull?
Metadata
Assignees
Labels
No labels
Activity