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

Maybe<T> has incorrect implementation of Equals, if T is a value type #614

Open
spirit11 opened this issue Jun 7, 2020 · 1 comment
Open

Comments

@spirit11
Copy link

spirit11 commented Jun 7, 2020

The implementation of Maybe class in The Maybe functor post https://blog.ploeh.dk/2018/03/26/the-maybe-functor/ has a little issue, when T is a value type. For example:

new Maybe<int>().Equals(new Maybe<int>(0))  // true

Maybe it is better to put a class constraint for T in Maybe<T>, or implement Equals like this?

	public override bool Equals(object obj)
	{
		var other = obj as Maybe<T>;
		if (other == null)
			return false;

		return !this.HasItem && !other.HasItem
			|| this.HasItem && other.HasItem && object.Equals(this.Item, other.Item);
	}
@ploeh
Copy link
Owner

ploeh commented Jun 7, 2020

Yes, good catch, that's a bug, it should check HasItem as well.

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

No branches or pull requests

2 participants