Swift: Please add pure methods to construct dictionaries in folds

Originator:rix.rob
Number:rdar://19841416 Date Originated:15-Feb-2015 06:23 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-Beta (6D520o)
Classification:Enhancement Reproducible:Always
 
Summary:
Sets have union as well as unionInPlace. Arrays have + as well as +=. Both Sets and Arrays are super convenient to construct in folds:

let unionOfElementThings = reduce(things, []) { $0.union($1.things) }
let flattened = reduce(arrayOfArrays, [], +)

Dictionaries, by contrast, have only mutating methods for this, so it gets tedious:

let thingValuesIndexedByKeys = reduce(things, [:]) { (var dict, each) in // you might need to give a complete type for the empty dictionary literal
    dict[each.key] = each.value
    return dict
}

This would be much nicer if Dictionary conformed to ExtensibleCollectionType, because then you’d get + for free:

let thingValuesIndexedByKeys = reduce(things, [:]) { $0 + $1.key, $1.value }

Taking it a step further, give Dictionary a constructor taking a sequence of (Key, Value) pairs. (Sort of like the existing DictionaryLiteralConvertible constructor, but not a variadic method which there’s still no splat operator for, argh, argh.) Then you can just + (Key, Value) tuples together into a dictionary:

let thingValuesIndexedByKeys = reduce(things, [:], +)

You can implement these in an extension, which is great, but extensions mean I have to re-do that everywhere because I can’t share those extensions between modules. I can’t do ExtensibleCollectionType conformance in an extension, either, because Dictionary is defined in a different module; so the only remaining option is duplication.


Steps to Reproduce:
N/A

Expected Results:
N/A

Actual Results:
N/A

Regression:
N/A

Notes:
N/A

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!