Where “where” May Be Used in Swift
The fact is you can use
where
keyword in a case label of aswitch
statement, acatch
clause of ado
statement, or in thecase
condition of anif
,while
,guard
,for-in
statement, or to define type constraints.
There are no Python-style list or dictionary comprehensions, though.
1 Comment RSS · Twitter
"There are no Python-style list or dictionary comprehensions, though."
Collections have map
and filter
methods. Not sure how efficient they are when chained together (ideally you don't want an entire temp array being generated inbetween each call), but eh. The biggest complaint is that applying `map` to a dictionary doesn't return a new dictionary but an array, though I imagine there's already a bunch of Radar tickets on that so hopefully it'll get sorted at some point.
..
OTOH, what's constantly being missed in this multitude of trees is the basic insanity of both Python and Swift sprouting all these duplicative doodads in the first place: `for` and `where`, which almost but not quite avoid the need for `map` and `filter`; and `map` and `filter`, which almost but not quite eliminate the need for `for` and `where`. (And in Python's case yet another option, `for...in...if`, which almost but not quite duplicates all the others and vice-versa.)
A good, parsimonious language design would pick one form (preferably the one that's the most composable), make sure it covers all of the relevant use cases, and blow away all the others, then get on with far more productive business of building interesting new layers of capability on top of that settled foundation. But mainstream language designers have this bad habit of fixating on primitive trivialities, so that by the time they're done creating a vast ingenious rats' nest out of those, nobody involved - language developers or language users - has the time or brain capacity left to manage anything more.
Frustrating.
--
p.s. My Swift duh of the day: case is Foo, is Bar
works perfectly while catch is Foo, is Bar
won't even compile. Go fig.