[swift-evolution] Revisiting SE-0110

Gwendal Roué gwendal.roue at gmail.com
Fri May 26 00:57:33 CDT 2017


> 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").

Here are below four examples of regressions introduced by SE-0110:

Example 1
-        return columns.index { (column, _) in column.lowercased() == lowercaseName }
+        return columns.index { $0.0.lowercased() == lowercaseName }

Example 2 :
-            .map { (mappedColumn, baseColumn) -> (Int, String) in
+            .map { (pair) -> (Int, String) in
+                let mappedColumn = pair.key
+                let baseColumn = pair.value

Example 3 :
-                .map { (table, columns) in "\(table)(\(columns.sorted().joined(separator: ", ")))" }		
+                .map { "\($0.key)(\($0.value.sorted().joined(separator: ", ")))" }

Example 4 :
-                dictionary.first { (column, value) in column.lowercased() == orderedColumn.lowercased() }
+                dictionary.first { $0.key.lowercased() == orderedColumn.lowercased() }

Gwendal Roué



More information about the swift-evolution mailing list