4 ms·
One of Swift's strengths is that most of its features really make sense if you read them, even if you aren't aware they existed. The entire language is optimize
by saagarjha 6y ago
One of Swift's strengths is that most of its features really make sense if you read them, even if you aren't aware they existed. The entire language is optimized around readability. (No, I'm not counting the function builder fiasco.) For example, you may not know that Swift's loops support where clauses:
for i in 1..<10 where i % 2 == 0 {
print(i)
}
But you can immediately understand how it works, because it's consistent with the rest of the language and reads well.
- kbenson 6y agoA where clause on a loop is nice, but I think it's likely it will either be abused or be limiting. I've always favored systems that work well together to build something more than the sum of the parts. Perl is an example of that in many cases. for my $i ( 1..(10-1) ) { next if $i % 2 == 0; print $i; } Here Perl's post-conditional syntax (which works only on a statement and not a block so it's more manageable) combines well with "next" (Perl's version of "continue") to very clearly convey intent without a lot of extra boilerplate. An additional clause is often just an additional line. Comments after each can provide additional context as needed. I think this is a more flexible way to accomplish the same thing, and more readable in all but the simplest of cases. And for those simplest of cases, I would use a grep, which is also useful in other parts of the language: for my $i ( grep { not $_ % 2 } 1..(10-1) ) { print $i; } Any syntax learned that works as a special case to only a single structure is either a case of missed opportunity or a case of extra syntax that needs to be learned which shouldn't be, IMO.
- saagarjha 6y agoThat where is not a one-off construct, it's used with switch statements: switch i { case 1...100 where i % 2 == 0: print("\(i) is a small even number") case ..<0: print("\(i) is negative") default: print("I couldn't care less") } Or generic constraints: struct Vector<T> where T: Numeric { // ... } If you so wish, you can always use any of the functional constructs as well, as in for i in (0..<10).filter { $0 % 2 == 0 } { print(i) }
- kbenson 6y agoSo it looks like it's a general modifier to a range type, so can be used where those work. That's slightly better, but if filter exists, why not just use that? One of the biggest criticisms that Perl gets is that there's so many different ways to do things, and that's somewhat deserved, so the question is what does "where" offer that "filter" doesn't other than another keyword and concept you have to learn to understand the language? What's the point if it's not really that much more expressive or clear? > struct Vector<T> where T: Numeric Is this really the same thing? This seems like a case where the same word is used to mean something else, even if they are loosely conceptually the same.
- jiofih 6y agoBetter learn a new, generic concept than obscure syntax I’ll see combined 1000 different ways on the same codebase.
- solidsnack9000 6y agoIt isn't actually a modifier to a range type -- it modifies other constructs, like looping or conditionals. The examples don't show it but literally any condition can go in the where clause, even something like `where random() % 7 == 2`.
- inopinatus 6y agoAny claim of the form "this language is magical! every statement is self-evident" has been bunk since the dawn of programming. The people making such claims are always already too close to the language to realise their own confirmation bias. So, feedback, from someone whose exposure to Swift consists basically of this article but has eaten a metric crapload of many other braced languages over the decades. What I can tell is that the programmer wanted to print 2,4,6,8. So the intentional readability is there. However when it comes to understanding how it works (i.e. the language mechanics delivering the intuitive reading) I had lexical concerns. I couldn't immediately tell whether the where-clause acts like a guard predicate in the for-statement or an unbraced if-statement i.e. equivalent to for i in 1..<10 { next if !(i % 2 == 0) print(i) } or for i in 1..<10 { if (i % 2 == 0) { print(i) } } or even (let's hope not) if (i % 2 == 0) { for i in 1..<10 { print(i) } } and in a similar vein the lexical scope and binding of i was not clear to me; some languages bind the iterated variable at the level of the statement, others inside the iterated body (which may even be a closure for all I know), and this has implications particularly for the the value of i after the loop, for the binding of i in any function generated inside the loop (example: print could actually talking to an IO object that defers output by prepending lambdas to a chain - I've seen web containers deferring view rendering this way), for name masking, and for the consequences of any flow-control/continuation/exception mechanism that may cause an early loop exit.
- saagarjha 6y agoI'm unsure what the difference is between the first two…are they not equivalent? The lexical scoping could be interpreted differently, sure (allowing your third example the benefit of the doubt) but this is where the "Swift is consistent" comes into play. You already know Swift's scoping rules, it's one of the first things you learn, so using that knowledge you can figure out this code as well.
- inopinatus 6y agoNo, they're not equivalent. See, that requires making an assumption that I didn't about lexical nesting. They merely do the same thing in this example. I do not know Swift's scoping rules and having read through the introductory guide and the flow control chapter I still can't tell you if the loop variable remains visible after the loop, and what it is bound to if so. No doubt that if I sat down with a REPL or an IDE it'd hopefully be evident by example within a few seconds, but the claim was about dry-reading, not interactive experience. I'll leave it there with an admonishment against making assumptions/declarations about the obvious-ness of something you're already familiar with.
- fiedzia 6y agoI find this style (Rust)) more readable end easier to extend: RangeInclusive::new(1, 10) .filter(|x| x % 2 == 0) .for_each(|x|{ println!("{}", x); }); (though you could write it the same way, for i in 1..=-10 { if .. { .. }}
- viktorcode 6y agoThe same can be written in Swift as (1...10).filter { $0.isMultiple(of: 2) }.forEach { print($0) }