Created
June 7, 2022 12:05
-
-
Save NikolajMosbaek/65d2c3904ba73e401cf1dfc7c6f4d1b2 to your computer and use it in GitHub Desktop.
This is how you can create the spaceship operator found in many other programming languages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator <=> | |
func <=><T: Comparable>(lhs: T, rhs: T) -> ComparisonResult { | |
if lhs < rhs { return .orderedAscending} | |
if lhs > rhs { return .orderedDescending } | |
return .orderedSame | |
} |
Usage:
let left = 5
let right = 6
let result = left <=> right
print(result.rawValue)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From HWS+: https://www.hackingwithswift.com/plus/intermediate-swift/creating-a-spaceship-operator