Re:Dictionary and Set Improvements in Swift 4.0
by Camillo on Thursday October 05, 2017 @12:09AM
(#15406637)
I agree, the examples in the article look terribly confusing. And they're inconsistent with Swift's own for loop syntax: https://developer.apple.com/library/content/documentation/Sw...
for item in groceries { print item.department }
versus: Dictionary(grouping: groceries by: { item in item.department })
The people who chose this syntax probably said "hey, it's VARIABLE in EXPRESSION, same thing right?". But the semantics are completely different!In the for loop it's "for PRODUCT in SOURCE": the expression is evaluated first, and the variable is assigned with each of its items in turn. In the lambda or whatever it is, it's "{ SOURCE in PRODUCT }": first the variable is assigned, then the expression is evaluated based on it. The data flow is the opposite!
This is just objectively bad language design.
Re:Dictionary and Set Improvements in Swift 4.0
by smitherfield on Thursday October 05, 2017 @12:45AM
(#15406723)
I prefer Ruby's syntax too, but Swift also has a shorthand version which is, IMO, usually nicer.
groceriesByDepartment.mapValues { $0.count }
Re:Dictionary and Set Improvements in Swift 4.0
by Groxx on Monday October 09, 2017 @06:41PM
(#15438032)
stuff like this always makes me miss Ruby's even-shorter-hand version though :)
groceriesByDepartment.mapValues(&:count) # not ruby, but meh
Re:Dictionary and Set Improvements in Swift 4.0
by jakobegger on Thursday October 05, 2017 @03:56AM
(#15407340)
The shorthand version doesn‘t let you specify argumen types (only works when they can be inferred)
E.g. this reads more naturally to me:
which would also move the "what it does" further to the left, rather than having to skip over the argument names (which are frequently obvious in context, and/or something trivial like "it" or "item" or "x").---
That said, if you consider it as "use 'items' in [a block of code]" it basically makes sense, and I could probably learn to stop worrying, and love the syntax.