csharp-algebraictypes icon indicating copy to clipboard operation
csharp-algebraictypes copied to clipboard

value == null returns false when value is null

Open Galad opened this issue 8 years ago • 0 comments

Description

The operator override of a discriminated union returns fase when the 2 operands are null.

Repro steps

  1. Create a discriminated union such as union Maybe<T>{ None | Some<T>}

  2. Use the following code:

    Maybe value = null; var result = (value == null);

Expected behavior

result is true

Actual behavior

result is false

Known workarounds

Use ReferenceEquals(value, null) when checking for null values.

Or

Modify the operator override in generated code with this: public static bool operator ==(Maybe<T> left, Maybe<T> right) => left?.Equals(right) ?? ReferenceEquals(right, default(T));

Related information

  • Release 1.0.1
  • .NET Framework 4.5

Galad avatar Jun 03 '17 13:06 Galad