Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
megafinz opened this issue Apr 3, 2017 · 5 comments
Closed

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

megafinz opened this issue Apr 3, 2017 · 5 comments
Labels
Milestone

Comments

@megafinz
Copy link

megafinz commented Apr 3, 2017

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 DavidArno added the Bug label Apr 12, 2017
@DavidArno DavidArno added this to the v3.0.0 milestone Apr 12, 2017
@DavidArno
Copy link
Owner

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
Copy link
Owner

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

@DavidArno
Copy link
Owner

Bug fix included in the v3.0.0 release.

@Opiumtm
Copy link

Opiumtm commented Jun 12, 2017

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants