csharp-algebraictypes
csharp-algebraictypes copied to clipboard
value == null returns false when value is null
Description
The operator override of a discriminated union returns fase when the 2 operands are null.
Repro steps
-
Create a discriminated union such as
union Maybe<T>{ None | Some<T>} -
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