Skip to content

Proposal/Discussion: "Damnit" Postfix Operator (!) #556

Closed

Description

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 warning

For 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)length

Open Questions:

  1. Should the ! operator cause an immediate exception if the value in question is null?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions