Roman Derkach
Roman Derkach

Reputation: 195

Overload >.. operator for function that creates Range excluding first index and includes last index does not work

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

Answers (1)

rob mayoff
rob mayoff

Reputation: 385670

UPDATE

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 >...

ORIGINAL

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:

operatordot-operator-head ­dot-operator-characters­opt
…snip…
dot-operator-head..­
dot-operator-character.­ | operator-character­
dot-operator-charactersdot-operator-character ­dot-operator-characters­opt­

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

Related Questions