DavidArno / SuccincT

Discriminated unions, pattern matching and partial applications for C#
MIT License
268 stars 15 forks source link

Comparing Option<T> to null doesn't work as expected #34

Closed megafinz closed 7 years ago

megafinz commented 7 years ago

Consider this code.

Option<string> a = null;
var isNull = a == null;

isNull equals false due to some type conversion magic:

1) Without the explicit type cast, compiler chooses public static bool operator ==(Option<T> a, Maybe<T> b) => (object) a != null && a.EqualsMaybe(b); overload for equality operator (Option{T}.cs).

2) "Free" null is cast first to Option<string> and then is wrapped into Maybe<string>. public static implicit operator Maybe<T>(Option<T> option) => new Maybe<T>(option); (Maybe{T}.cs)

3) Equality operator returns false since first operand is null.

DavidArno commented 7 years ago

Looking at the code around the == operators for Option<T> and Maybe<T>, I think maybe I'd had at least ten too many beers when writing that stuff! 😨 It looks like a classic example of "just keep adding more crap until the tests pass".

Time for a serious bit of refactoring...

DavidArno commented 7 years ago

Accidentally checked in the fix for this with the commit of the 4th May. Comparisons between options, maybes and null are greatly simplified.

DavidArno commented 7 years ago

Bug fix included in the v3.0.0 release.

Opiumtm commented 7 years ago

It's not as simple as it seems.

SQL null comparison rules state that any comparison to null value returns false. According to SQL rules, null = null check result is false.

It's not clear which null comparison standard is "true".

megafinz commented 7 years ago

It's not clear which null comparison standard is "true".

How is that? Why do we care about SQL rules? In CLR world comparing one null reference to another null reference is determined to return "true" (https://github.com/dotnet/csharplang/blob/master/spec/types.md#the-bool-type).

In the C and C++ languages, a zero integral or floating-point value, or a null pointer can be converted to the boolean value false, and a non-zero integral or floating-point value, or a non-null pointer can be converted to the boolean value true. In C#, such conversions are accomplished by explicitly comparing an integral or floating-point value to zero, or by explicitly comparing an object reference to null.