Reputation: 195
I want to create global function that creates range. It will be analog for the ..> function. But I cannot do that. XCode shows me a lot of errors when I introduce him this code
infix operator >.. {
associativity none
precedence 135
}
@warn_unused_result
func >..<Pos : ForwardIndexType where Pos : Comparable>(start: Pos, end: Pos) -> Range<Pos> {
return Range(start: start.successor(), end: end)
}
It shows me strange errors that try to change my syntax. Exception: when I change operator to *** for example - it works. But this operator is not good for this case of function.
Upvotes: 0
Views: 367
Reputation: 385670
The Swift compiler that is part of Xcode 7.2 only allows an operator to contain dots if it either contains only dots (minimum two) or is exactly ..<
. See swift/Lexer.cpp
. I opened a bug report about it.
Then I investigated further and found that Chris Lattner rewrote that part of the code on Dec 17, 2015 to be more flexible. Quoting the commit message:
The policy is now very simple: if an operator name starts with a dot, it is allowed to include other dots in its name. If it doesn't, it doesn't.
So expect some future version of Swift (not Xcode 7.3-beta, I checked) to allow you to define an operator that starts with dots and other doodads, but still not >..
.
The only way to include dots in a Swift operator is to start the operator with two dots. Here's the relevant part of the Swift grammar:
operator → dot-operator-head dot-operator-charactersopt
…snip…
dot-operator-head →..
dot-operator-character →.
| operator-character
dot-operator-characters → dot-operator-character dot-operator-charactersopt
Since .
is not included in any other part of the operator grammar, you cannot include a .
anywhere in an operator unless the operator begins with ..
.
You can find the full operator grammar in The Swift Programming Language: “Operators”.
Upvotes: 1