Skip to content

Instantly share code, notes, and snippets.

@eddiekaiger
Last active October 4, 2016 01:52
Show Gist options
  • Save eddiekaiger/134d9ba36a65ef49628634cd339cdf53 to your computer and use it in GitHub Desktop.
Save eddiekaiger/134d9ba36a65ef49628634cd339cdf53 to your computer and use it in GitHub Desktop.
TypeComparable
//
// TypeComparable
//
// Created by Eddie Kaiger on 10/3/16.
// Copyright (c) 2016 Eddie Kaiger. All rights reserved.
//
import Foundation
/**
A protocol that enables equality checks between two different types.
*/
public protocol TypeEquatable {
associatedtype OtherType
static func == (lhs: Self, rhs: OtherType) -> Bool
static func == (lhs: OtherType, rhs: Self) -> Bool
}
/**
A protocol that enables comparison between two different types.
*/
public protocol TypeComparable: TypeEquatable {
static func < (lhs: Self, rhs: OtherType) -> Bool
static func < (lhs: OtherType, rhs: Self) -> Bool
static func > (lhs: Self, rhs: OtherType) -> Bool
static func > (lhs: OtherType, rhs: Self) -> Bool
static func <= (lhs: Self, rhs: OtherType) -> Bool
static func <= (lhs: OtherType, rhs: Self) -> Bool
static func >= (lhs: Self, rhs: OtherType) -> Bool
static func >= (lhs: OtherType, rhs: Self) -> Bool
}
// MARK: - Equal
public func == <LeftType, RightType: TypeEquatable>(lhs: LeftType, rhs: RightType) -> Bool where RightType.OtherType == LeftType {
return rhs == lhs
}
// MARK: - Not Equal
public func != <LeftType: TypeEquatable, RightType>(lhs: LeftType, rhs: RightType) -> Bool where LeftType.OtherType == RightType {
return !(lhs == rhs)
}
public func != <LeftType, RightType: TypeEquatable>(lhs: LeftType, rhs: RightType) -> Bool where RightType.OtherType == LeftType {
return rhs != lhs
}
// MARK: - Greater Than
public func > <LeftType: TypeComparable, RightType>(lhs: LeftType, rhs: RightType) -> Bool where LeftType.OtherType == RightType {
return lhs != rhs && !(lhs < rhs)
}
public func > <LeftType, RightType: TypeComparable>(lhs: LeftType, rhs: RightType) -> Bool where RightType.OtherType == LeftType {
return lhs != rhs && !(lhs < rhs)
}
// MARK: - Less Than Or Equal
public func <= <LeftType: TypeComparable, RightType>(lhs: LeftType, rhs: RightType) -> Bool where LeftType.OtherType == RightType {
return lhs == rhs || lhs < rhs
}
public func <= <LeftType, RightType: TypeComparable>(lhs: LeftType, rhs: RightType) -> Bool where RightType.OtherType == LeftType {
return lhs == rhs || lhs < rhs
}
// MARK: - Greater Than Or Equal
public func >= <LeftType: TypeComparable, RightType>(lhs: LeftType, rhs: RightType) -> Bool where LeftType.OtherType == RightType {
return lhs == rhs || lhs > rhs
}
public func >= <LeftType, RightType: TypeComparable>(lhs: LeftType, rhs: RightType) -> Bool where RightType.OtherType == LeftType {
return lhs == rhs || lhs > rhs
}
// Example with comparing an NSNumber to an Int
extension NSNumber: TypeComparable {
public typealias OtherType = Int
}
public func == (lhs: NSNumber, rhs: Int) -> Bool {
return lhs.intValue == rhs
}
public func < (lhs: NSNumber, rhs: Int) -> Bool {
return lhs.intValue < rhs
}
public func < (lhs: Int, rhs: NSNumber) -> Bool {
return lhs < rhs.intValue
}
let x: NSNumber = 5
let y: Int = 6
x < y
x > y
x <= y
x >= y
x == y
x != y
y < x
y > x
y <= x
y >= x
y == x
y != x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment