3 ms·
Don't forget the removal of the C-style for loop, along with the increment ++ and decrement -- operators. https://github.com/apple/swift-evolution/blob/master/
by alblue 11y ago
Don't forget the removal of the C-style for loop, along with the increment ++ and decrement -- operators.
https://github.com/apple/swift-evolution/blob/master/proposals/0007-remove-c-style-for-loops.md https://github.com/apple/swift-evolution/blob/master/proposa...
https://github.com/apple/swift-evolution/blob/master/proposals/0004-remove-pre-post-inc-decrement.md https://github.com/apple/swift-evolution/blob/master/proposa...
- brudgers 11y agoThe one's I linked to are listed as changes incorporated into Swift 2.2 per the announcement.
- whiddershins 11y agoI am baffled by the for loop removal thing. I just spent a while reading about both stride and for ... in, and I feel like there are any number of cases where I would rather use a C-style for loop.
- xxpor 11y agoI'm not familiar with swift, but Python doesn't have C style for loops either. People just write: for i in range(10): instead.
- vram22 11y agoMore generally, people write: for item in sequence: which is somewhat general (sequences include lists, dicts, strings, tuples, generators, text files and more [2]), and it covers a wide range [1] of use cases. [1] Pun not intended. [2] Basically, any iterable. This include custom iterables you can define, which can then be iterated over using the same standard for loop. A unifying feature of the language: "The use of iterators pervades and unifies Python." https://docs.python.org/2/tutorial/classes.html#iterators https://docs.python.org/2/tutorial/classes.html#iterators
- rat87 11y agowhat sort of cases? Could you use a while loop instead? I feel like (as long as the numerical case optimizes to the same thing) the for-in loop (and occasional use of the while loop) is clearer.
- oscargrouch 11y agoTheres only a case here where iterating over a parent/child relationship, and testing for nullability, its more clear with the c-style for than with a while loop: // old for x: Thing? = thing; x != nil; x = x.parent { ... } // and now var x: Thing? = thing while x != nil { ... x = x.parent } It was the only case i've really missed it.. unless theres a better way to do it that im not aware of.. But the for c-style loop for numbers is really not needed
- rat87 11y agoIdeally that sort of relationship should probably be an iterator, though it's still useful for internal type implementation methods.
- cballard 11y agoI am not sure if there is a name for this type of function, so I called it someFunction: func someFunction<T>(initial: T, next: T -> T?) -> T { var current = initial while true { switch next(current) { case .Some(let value): current = value case .None: return current } } } Now you can express that idea generally, e.g.: extension UIView { var rootView: UIView { return someFunction(self, next: { view in view.superview }) } } I also didn't test it, so it might not work. Of course, it would be more Swifty to create a "Parentable" protocol and write that as an extension of it!