News for hackers. Stuff that matters.
Re:Dictionary and Set Improvements in Swift 4.0 by thedjinn on Wednesday October 04, 2017 @06:14PM (#15405186)
Looks fine to me. What's wrong about that syntax?
Re:Dictionary and Set Improvements in Swift 4.0 by Groxx on Wednesday October 04, 2017 @08:23PM (#15405936)
Feels backwards to me, at least as a gut feel.

    groceriesByDepartment.mapValues { items in items.count }
vs e.g. Ruby (or at least close, it has been a while):

    groceriesByDepartment.map { |item| item.count }
The "in" makes it feel like you're calling "items.count" on...? and then getting the "items" from it (since it sorta implies that items are in items.count, which seems like nonsense).

E.g. this reads more naturally to me:

    groceriesByDepartment.mapValues { item.count in item }
    otherStuff.do { a.value + b.value in a, b }
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.

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)
Re:Dictionary and Set Improvements in Swift 4.0 by mantas on Thursday October 05, 2017 @05:20AM (#15407725)
Which is vast majority of cases. Unless you work with a lot of legacy ObjC code.
Re:Dictionary and Set Improvements in Swift 4.0 by BillinghamJ on Wednesday October 04, 2017 @07:25PM (#15405655)
Well, for starters, what does it mean? I haven’t seen that in any other language.
Re:Dictionary and Set Improvements in Swift 4.0 by pjmlp on Wednesday October 04, 2017 @07:35PM (#15405704)
It is similar to let var=..... in exp from ML languages.
Re:Dictionary and Set Improvements in Swift 4.0 by sizeofchar on Thursday October 05, 2017 @02:25AM (#15406997)
Haskell have let-in expressions, I think it is the inspiration for swift in this case.