I know how to customize binary operators, like this
infix operator ** { associativity left precedence 170 }
func ** (left: Double, right: Double) -> Doubl
precedencegroup SecondaryTernaryPrecedence {
associativity: right
higherThan: TernaryPrecedence
lowerThan: LogicalDisjunctionPrecedence
}
infix operator ~ : SecondaryTernaryPrecedence
func ~ (lhs: @autoclosure () -> Bool, rhs: @escaping @autoclosure () -> T) -> (Bool, () -> T) {
return (lhs(), rhs)
}
infix operator >< : TernaryPrecedence
@discardableResult func >< (lhs: (Bool, () -> T), rhs: @escaping @autoclosure () -> T) -> T {
if lhs.0 {
return lhs.1()
} else {
return rhs()
}
}
let n = false ~ "it was true" >< "it was false" //"it was false"
true ~ print("it was true") >< print("it was false")
// Prints "it was true"
Note: While this is not a true ternary operator per se, as it uses two infix operators in conjunction with one another, it does in fact somewhat emulate its behaviour when the two operators are used together in the fashion presented above.