Skip to content

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

Description

@spirit11

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);
	}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions