Hello Swift Community, thanks to all of you for this nice programming language
Let's create a ∈ operator (that I liked to be called "in") :
enum CompassPoint: Equatable {
case north
case south
case east
case west
}
infix operator ∈
extension Sequence where Element: Equatable {
public static func ∈ (left: Element, right: Self) -> Bool {
return right.contains(left)
}
}
let point = CompassPoint.north
let validPoints = [CompassPoint.north, .south]
// validPoints.contains(point)
if point ∈ validPoints {
print("\(point) is valid")
} else {
print("\(point) is not valid")
}
I can now write code like this:
if point in [CompassPoint.north, .south] {
As we have the for-in, I think the if-in could be valuable as it is highly readable way to look if an item is in an Array
Side question about negation of this test, does 'not in' looks a nice idea:
if point not in [CompassPoint.north, .south] {
or maybe just use ! before the test, the 'not in' is more readable to me
I don't know how eveyone else but most people who started their programming experience in Pascal, <> is an operator for "not equal", i.e. what is in C-based languages known as !=. Not sure if that's relevant, but to me the operator is not self-explanatory and with my background, it's even confusing...
My intuition tells me the idea of having multi-purpose keywords is rather picky, but in terms of ergonomics, it does look more neat, especially for array literals, than contains. With in, that is, not the operator. As an option, this can be implemented as sugar for
for foo in sequence where foo == point {
...
break;
}
and replaced accordingly during IRGen without further tying the compiler to the stdlib. This, however, doesn't take advantage of the performance dependent contains method.
If we were going to add this, I think it would be an overload of the in operator. But I think this may fall under the commonly rejected list along with "and" and "or"
It can be both. Just like as is a keyword in the Swift language, it represents both the casting operator, as well as functioning as a pattern match casting operator. In most cases in in Swift could be called the range or iterator operator.
One of my favorite memories from my programming language classes in college was the professor asking what new was in the Java language. It took us about 5 minutes and much scolding from the professor before we came up with the answer, "operator". In Java it's the constructor operator.
if-in could possibly make sense as a sort of generalisation of for-in. It's not a very high priority though, as the desugared form is straightforward compared to for-in, which has to make the iterator and advance it. In other words:
for item in items { … }
// versus
var itemsIterator = items.makeIterator()
while let item = itemsIterator.next() { … }
is a lot more onerous than
if item in items { … }
// versus
if items.contains(item) { … }