Skip to content

Instantly share code, notes, and snippets.

@Nadohs
Last active December 30, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nadohs/308603afa65cbbfba07c to your computer and use it in GitHub Desktop.
Save Nadohs/308603afa65cbbfba07c to your computer and use it in GitHub Desktop.

###Subject: isEqual to replace == Equatable Requirement

Hi all,

I would like to propose changing the Equatable protocol to use isEqual(to:Self) -> Bool, defined inside of a type to replace the currently used operator == function, which is defined outside of the type.

###Reasoning:

  1. Having the conforming function defined inside of the type is more intuitive, since in general functions required for conformance are defined within the type. It feels like an unnecesary detail for learners of Swift to have to stumble through.

The implementation for this would look something like this:

	 public protocol Equatable{
	   ....

	   /// Shortcut for defining `==` function inside type definition.
	   @warn_unused_result
	   func isEqual(to:Self) -> Bool
	 }
	 
	 @warn_unused_result
	 public func == <T : Equatable>(lhs: T, rhs: T) -> Bool {
	     return lhs.isEqual(rhs)
	 }

###Impact on Existing Code: This implementation would break existing code, but could be fixed with a default protocol extension such as:

	 /// Default `isEqual` function to satisfy other types only definiting
	 /// `==` for equality.
	 public extension Equatable{
	     func isEqual(to:Self) -> Bool{
	         return self == to
	     }
	 }	

Not adding the default function for isEqual makes more sense to me though, since it would remove any requirement for Equatable conformance and leave no warning for the loop you would create by implementing neither isEqual nor ==.

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