https://github.com/apple/swift-evolution/blob/master/proposa...
https://github.com/apple/swift-evolution/blob/master/proposa...
https://github.com/apple/swift-evolution/blob/master/proposa...
https://github.com/apple/swift-evolution/blob/master/proposa...
https://github.com/apple/swift-evolution/blob/master/proposa...
https://github.com/apple/swift-evolution/blob/master/proposa...
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.
for i in range(10):
instead.
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."
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.
// 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
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!
In Swift 3.0 they'll remove the deprecated syntaxes.