Swift should officially allow Unicode custom operators
| Originator: | jlieske | ||
| Number: | rdar://17265161 | Date Originated: | 11-Jun-2014 00:06 AM |
| Status: | Closed | Resolved: | Resolved |
| Product: | Developer Tools | Product Version: | Xcode6 beta: Version 6.0 (6A215l) |
| Classification: | Enhancement | Reproducible: | Always |
Swift officially only supports custom operators using a small set of characters. However, the current Swift compiler accepts many Unicode characters for custom operators. This allows some elegant constructions:
operator infix ≤ {associativity none precedence 130}
func ≤<T : Comparable>(a: T, b: T) -> Bool {
return a <= b
}
println(1 ≤ 2)
operator infix → {associativity left precedence 105}
func →<ArgType,ResultType>(arg: ArgType, f: (ArgType) -> ResultType) -> ResultType {
return f(arg)
}
"arrow" → println
operator infix ∑ {associativity none precedence 125}
func ∑(sequence: Double[], transform: (Double) -> Double) -> Double {
return reduce(map(sequence, transform), 0, +)
}
println([1, 2, 3, 4] ∑ {$0*$0})
I request that the current support of Unicode custom operators become an official part of the language.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
This was documented in Swift 1.0.