Skip to content

Instantly share code, notes, and snippets.

@jbrjake
Last active May 5, 2016 05:05
Show Gist options
  • Save jbrjake/b3662bf251bd6a62b36d479a066dc426 to your computer and use it in GitHub Desktop.
Save jbrjake/b3662bf251bd6a62b36d479a066dc426 to your computer and use it in GitHub Desktop.
From laziness, vanity
//: Playground - noun: a place where people can play
import UIKit
protocol AnyEquatable {
func equals(otherObject: AnyEquatable) -> Bool
}
extension AnyEquatable {
func equals(otherObject: AnyEquatable) -> Bool {
return false
}
}
extension AnyEquatable where Self : Equatable {
// otherObject could also be 'Any'
func equals(otherObject: AnyEquatable) -> Bool {
if let otherAsSelf = otherObject as? Self {
return otherAsSelf == self
}
return false
}
}
extension Int :AnyEquatable {}
extension Float :AnyEquatable {}
extension String :AnyEquatable {}
extension Array :AnyEquatable {}
extension Dictionary :AnyEquatable {}
protocol AutoEquatable :Equatable {}
func ==<T: AutoEquatable>(lhs: T, rhs: T) -> Bool {
let reLhs = Mirror(reflecting: lhs)
let reRhs = Mirror(reflecting: rhs)
var theSame = true
if reLhs.children.count == 0 {
return false
}
for (leftKid, rightKid) in zip(reLhs.children, reRhs.children) {
if let
leftKid = leftKid.value as? AnyEquatable,
rightKid = rightKid.value as? AnyEquatable
{
theSame = leftKid.equals(rightKid)
}
}
return theSame
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment