[swift-evolution] Revisiting SE-0110
Gwendal Roué
gwendal.roue at gmail.com
Sun May 28 02:24:09 CDT 2017
> Le 27 mai 2017 à 19:43, Dave Abrahams via swift-evolution <swift-evolution at swift.org> a écrit :
>
>
> on Thu May 25 2017, Gwendal Roué <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>
>>> Furthermore, this probably comes up most commonly with dictionaries,
>>> since they're a sequence of tuples. The element tuple for
>>> dictionaries has element labels (key: Key, value: Value), so instead
>>> of writing `{ tuple in let (key, value) = tuple; f(key, value) }`,
>>> you could use the implicit argument and write `{ f($0.key, $0.value)
>>> }`.
>>>
>>> -Joe
>>
>> I've migrated a project from Swift 3 to Swift 4 (relevant commit:
>> https://github.com/groue/GRDB.swift/commit/4f26cbcacf7b783c9c503f2909f2eb03ef7930fe)
>>
>> Joe is right, dictionaries, as handy as they are, are particularly affected. But $0 is hardly a
>> panacea.
>>
>> What I regret the most with the change is the lost ability to give
>> *relevant names* to tuple elements (and sometimes with the forced
>> introduction of a phony variable that has no relevant name (like
>> "pair").
>
> Not saying there's no problem here, but `kv` (or `keyValue` if you must)
> works pretty well for this. At least it isn't purely a reflection of
> the type and gives some hint as to semantics.
Let me please try to be understood:
A dictionary is a convenience type, with many many purposes. One rarely thinks of a dictionary as a "dictionary". It's more often a "cache", or a "name1 to name2 mapping", as in "name to position mapping", "lowercase string to case-preserved string mapping", or whatever you want.
The "kv", "pair", "key", "value" identifiers are not only irrelevant, they actively hinder code readability. Compare:
- ...map { $0.key ... $0.value ... }
- ...map { (name, position) in name ... position ... }
Gwendal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170528/64ac7523/attachment.html>
More information about the swift-evolution
mailing list