Skip to content

Instantly share code, notes, and snippets.

@ftchirou
Last active Dec 17, 2019
Embed
What would you like to do?
Comparison operations to compare Primitive values (https://gist.github.com/ftchirou/989f9cc8293ea5a0e72200fae51ae5af) and Any values.
internal func == (lhs: Any, rhs: Primitive) -> Bool {
return isPrimitive(rhs, ofType: rhs.type, equalTo: lhs)
}
internal func < (lhs: Any, rhs: Primitive) -> Bool {
return isPrimitive(rhs, ofType: rhs.type, lessThan: lhs)
}
internal func <= (lhs: Any, rhs: Primitive) -> Bool {
return lhs == rhs || lhs < rhs
}
internal func > (lhs: Any, rhs: Primitive) -> Bool {
return isPrimitive(lhs, ofType: rhs.type, lessThan: lhs)
}
internal func >= (lhs: Any, rhs: Primitive) -> Bool {
return lhs == rhs || lhs > rhs
}
private func isPrimitive(_ value: Any, ofType type: Type, equalTo other: Any) -> Bool {
switch type {
case .bool:
guard let lhs = value as? Bool, let rhs = other as? Bool else { return false }
return lhs == rhs
case .int:
guard let lhs = value as? Int, let rhs = other as? Int else { return false }
return lhs == rhs
case .int8:
guard let lhs = value as? Int8, let rhs = other as? Int8 else { return false }
return lhs == rhs
case .int16:
guard let lhs = value as? Int16, let rhs = other as? Int16 else { return false }
return lhs == rhs
case .int32:
guard let lhs = value as? Int32, let rhs = other as? Int32 else { return false }
return lhs == rhs
case .int64:
guard let lhs = value as? Int64, let rhs = other as? Int64 else { return false }
return lhs == rhs
case .uint:
guard let lhs = value as? UInt, let rhs = other as? UInt else { return false }
return lhs == rhs
case .uint8:
guard let lhs = value as? UInt8, let rhs = other as? UInt8 else { return false }
return lhs == rhs
case .uint16:
guard let lhs = value as? UInt16, let rhs = other as? UInt16 else { return false }
return lhs == rhs
case .uint32:
guard let lhs = value as? UInt32, let rhs = other as? UInt32 else { return false }
return lhs == rhs
case .uint64:
guard let lhs = value as? UInt64, let rhs = other as? UInt64 else { return false }
return lhs == rhs
case .double:
guard let lhs = value as? Double, let rhs = other as? Double else { return false }
return lhs == rhs
case .float:
guard let lhs = value as? Float, let rhs = other as? Float else { return false }
return lhs == rhs
case .float80:
guard let lhs = value as? Float80, let rhs = other as? Float80 else { return false }
return lhs == rhs
case .cgFloat:
guard let lhs = value as? CGFloat, let rhs = other as? CGFloat else { return false }
return lhs == rhs
case .string:
guard let lhs = value as? String, let rhs = other as? String else { return false }
return lhs == rhs
case .date:
guard let lhs = value as? Date, let rhs = other as? Date else { return false }
return lhs == rhs
case let .wrapped(value, type):
return isPrimitive(value, ofType: type, equalTo: other)
}
}
private func isPrimitive(_ value: Any, ofType type: Type, lessThan other: Any) -> Bool {
switch type {
case .bool:
guard let lhs = value as? Bool, let rhs = other as? Bool else { return false }
return lhs < rhs
case .int:
guard let lhs = value as? Int, let rhs = other as? Int else { return false }
return lhs < rhs
case .int8:
guard let lhs = value as? Int8, let rhs = other as? Int8 else { return false }
return lhs < rhs
case .int16:
guard let lhs = value as? Int16, let rhs = other as? Int16 else { return false }
return lhs < rhs
case .int32:
guard let lhs = value as? Int32, let rhs = other as? Int32 else { return false }
return lhs < rhs
case .int64:
guard let lhs = value as? Int64, let rhs = other as? Int64 else { return false }
return lhs < rhs
case .uint:
guard let lhs = value as? UInt, let rhs = other as? UInt else { return false }
return lhs < rhs
case .uint8:
guard let lhs = value as? UInt8, let rhs = other as? UInt8 else { return false }
return lhs < rhs
case .uint16:
guard let lhs = value as? UInt16, let rhs = other as? UInt16 else { return false }
return lhs < rhs
case .uint32:
guard let lhs = value as? UInt32, let rhs = other as? UInt32 else { return false }
return lhs < rhs
case .uint64:
guard let lhs = value as? UInt64, let rhs = other as? UInt64 else { return false }
return lhs < rhs
case .double:
guard let lhs = value as? Double, let rhs = other as? Double else { return false }
return lhs < rhs
case .float:
guard let lhs = value as? Float, let rhs = other as? Float else { return false }
return lhs < rhs
case .float80:
guard let lhs = value as? Float80, let rhs = other as? Float80 else { return false }
return lhs < rhs
case .cgFloat:
guard let lhs = value as? CGFloat, let rhs = other as? CGFloat else { return false }
return lhs < rhs
case .string:
guard let lhs = value as? String, let rhs = other as? String else { return false }
return lhs < rhs
case .date:
guard let lhs = value as? Date, let rhs = other as? Date else { return false }
return lhs < rhs
case let .wrapped(value, type):
return isPrimitive(value, ofType: type, lessThan: other)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment