Skip to content

Renamed DictionaryLiteral to KeyValuePairs #16577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ CHANGELOG
Swift 5.0
---------

* [SE-0214][]:

Renamed the `DictionaryLiteral` type to `KeyValuePairs`.
A typealias preserves the old name for compatibility.

* [SR-2608][]

Default arguments are now printed in SourceKit-generated interfaces for Swift
Expand Down Expand Up @@ -7147,6 +7152,20 @@ Swift 1.0
[SE-0210]: <https://github.com/apple/swift-evolution/blob/master/proposals/0210-key-path-offset.md>
[SE-0211]: <https://github.com/apple/swift-evolution/blob/master/proposals/0211-unicode-scalar-properties.md>
[SE-0212]: <https://github.com/apple/swift-evolution/blob/master/proposals/0212-compiler-version-directive.md>
[SE-0213]: <https://github.com/apple/swift-evolution/blob/master/proposals/0213-literal-init-via-coercion.md>
[SE-0214]: <https://github.com/apple/swift-evolution/blob/master/proposals/0214-DictionaryLiteral.md>
[SE-0215]: <https://github.com/apple/swift-evolution/blob/master/proposals/0215-conform-never-to-hashable-and-equatable.md>
[SE-0216]: <https://github.com/apple/swift-evolution/blob/master/proposals/0216-dynamic-callable.md>
[SE-0217]: <https://github.com/apple/swift-evolution/blob/master/proposals/0217-bangbang.md>
[SE-0218]: <https://github.com/apple/swift-evolution/blob/master/proposals/0218-introduce-compact-map-values.md>
[SE-0219]: <https://github.com/apple/swift-evolution/blob/master/proposals/0219-package-manager-dependency-mirroring.md>
[SE-0220]: <https://github.com/apple/swift-evolution/blob/master/proposals/0220-count-where.md>
[SE-0221]: <https://github.com/apple/swift-evolution/blob/master/proposals/0221-character-properties.md>
[SE-0222]: <https://github.com/apple/swift-evolution/blob/master/proposals/0222-lazy-compactmap-sequence.md>
[SE-0223]: <https://github.com/apple/swift-evolution/blob/master/proposals/0223-array-uninitialized-initializer.md>
[SE-0224]: <https://github.com/apple/swift-evolution/blob/master/proposals/0224-ifswift-lessthan-operator.md>
[SE-0225]: <https://github.com/apple/swift-evolution/blob/master/proposals/0225-binaryinteger-iseven-isodd-ismultiple.md>
[SE-0226]: <https://github.com/apple/swift-evolution/blob/master/proposals/0226-package-manager-target-based-dep-resolution.md>

[SR-106]: <https://bugs.swift.org/browse/SR-106>
[SR-419]: <https://bugs.swift.org/browse/SR-419>
Expand Down
2 changes: 1 addition & 1 deletion benchmark/single-source/RomanNumbers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public let RomanNumbers = [
tags: [.api, .String, .algorithm])
]

let romanTable: DictionaryLiteral<String, Int> = [
let romanTable: KeyValuePairs<String, Int> = [
"M": 1000,
"CM": 900,
"D": 500,
Expand Down
2 changes: 1 addition & 1 deletion benchmark/single-source/StringMatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func matchStar(_ c: Character, _ regexp: Substring, _ text: Substring) -> Bool {
}
}

let tests: DictionaryLiteral = [
let tests: KeyValuePairs = [
"^h..lo*!$":"hellooooo!",
"^h..lo*!$":"hella noms",
".ab":"abracadabra!",
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class alignas(8) Expr {
IsTypeDefaulted : 1,
/// Number of comma source locations.
NumCommas : 32 - 1 - NumExprBits,
/// Number of entries in the collection. If this is a DictionaryLiteral,
/// Number of entries in the collection. If this is a DictionaryExpr,
/// each entry is a Tuple with the key and value pair.
NumSubExprs : 32
);
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/StdlibUnittest/TypeIndexed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public func <=> <T: Comparable>(
}

public func expectEqual<V: Comparable>(
_ expected: DictionaryLiteral<Any.Type, V>, _ actual: TypeIndexed<V>,
_ expected: KeyValuePairs<Any.Type, V>, _ actual: TypeIndexed<V>,
_ message: @autoclosure () -> String = "",
showFrame: Bool = true,
stackTrace: SourceLocStack = SourceLocStack(),
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/core/CompilerProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,10 @@ public protocol ExpressibleByArrayLiteral {
/// print(frequencies.count)
/// // Prints "0"
///
/// - Note: A dictionary literal is *not* the same as an instance of
/// `Dictionary` or the similarly named `DictionaryLiteral` type. You can't
/// initialize a type that conforms to `ExpressibleByDictionaryLiteral` simply
/// by assigning an instance of one of these types.
/// - Note:
/// A dictionary literal is *not* the same as an instance of `Dictionary`.
/// You can't initialize a type that conforms to `ExpressibleByDictionaryLiteral`
/// simply by assigning an instance of `Dictionary`, `KeyValuePairs`, or similar.
///
/// Conforming to the ExpressibleByDictionaryLiteral Protocol
/// =========================================================
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ import SwiftShims
/// The order of key-value pairs in a dictionary is stable between mutations
/// but is otherwise unpredictable. If you need an ordered collection of
/// key-value pairs and don't need the fast key lookup that `Dictionary`
/// provides, see the `DictionaryLiteral` type for an alternative.
/// provides, see the `KeyValuePairs` type for an alternative.
///
/// You can search a dictionary's contents for a particular value using the
/// `contains(where:)` or `firstIndex(where:)` methods supplied by default
Expand Down
41 changes: 22 additions & 19 deletions stdlib/public/core/Mirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public struct Mirror {
/// `subject` is not a class instance. The default is `.generated`.
public init<Subject>(
_ subject: Subject,
children: DictionaryLiteral<String, Any>,
children: KeyValuePairs<String, Any>,
displayStyle: DisplayStyle? = nil,
ancestorRepresentation: AncestorRepresentation = .generated
) {
Expand Down Expand Up @@ -464,25 +464,25 @@ extension Mirror {

/// A lightweight collection of key-value pairs.
///
/// Use a `DictionaryLiteral` instance when you need an ordered collection of
/// Use a `KeyValuePairs` instance when you need an ordered collection of
/// key-value pairs and don't require the fast key lookup that the
/// `Dictionary` type provides. Unlike key-value pairs in a true dictionary,
/// neither the key nor the value of a `DictionaryLiteral` instance must
/// neither the key nor the value of a `KeyValuePairs` instance must
/// conform to the `Hashable` protocol.
///
/// You initialize a `DictionaryLiteral` instance using a Swift dictionary
/// You initialize a `KeyValuePairs` instance using a Swift dictionary
/// literal. Besides maintaining the order of the original dictionary literal,
/// `DictionaryLiteral` also allows duplicates keys. For example:
/// `KeyValuePairs` also allows duplicates keys. For example:
///
/// let recordTimes: DictionaryLiteral = ["Florence Griffith-Joyner": 10.49,
/// let recordTimes: KeyValuePairs = ["Florence Griffith-Joyner": 10.49,
/// "Evelyn Ashford": 10.76,
/// "Evelyn Ashford": 10.79,
/// "Marlies Gohr": 10.81]
/// print(recordTimes.first!)
/// // Prints "("Florence Griffith-Joyner", 10.49)"
///
/// Some operations that are efficient on a dictionary are slower when using
/// `DictionaryLiteral`. In particular, to find the value matching a key, you
/// `KeyValuePairs`. In particular, to find the value matching a key, you
/// must search through every element of the collection. The call to
/// `firstIndex(where:)` in the following example must traverse the whole
/// collection to find the element that matches the predicate:
Expand All @@ -499,67 +499,70 @@ extension Mirror {
/// Dictionary Literals as Function Parameters
/// ------------------------------------------
///
/// When calling a function with a `DictionaryLiteral` parameter, you can pass
/// When calling a function with a `KeyValuePairs` parameter, you can pass
/// a Swift dictionary literal without causing a `Dictionary` to be created.
/// This capability can be especially important when the order of elements in
/// the literal is significant.
///
/// For example, you could create an `IntPairs` structure that holds a list of
/// two-integer tuples and use an initializer that accepts a
/// `DictionaryLiteral` instance.
/// `KeyValuePairs` instance.
///
/// struct IntPairs {
/// var elements: [(Int, Int)]
///
/// init(_ elements: DictionaryLiteral<Int, Int>) {
/// init(_ elements: KeyValuePairs<Int, Int>) {
/// self.elements = Array(elements)
/// }
/// }
///
/// When you're ready to create a new `IntPairs` instance, use a dictionary
/// literal as the parameter to the `IntPairs` initializer. The
/// `DictionaryLiteral` instance preserves the order of the elements as
/// `KeyValuePairs` instance preserves the order of the elements as
/// passed.
///
/// let pairs = IntPairs([1: 2, 1: 1, 3: 4, 2: 1])
/// print(pairs.elements)
/// // Prints "[(1, 2), (1, 1), (3, 4), (2, 1)]"
@_fixed_layout // FIXME(sil-serialize-all)
public struct DictionaryLiteral<Key, Value> : ExpressibleByDictionaryLiteral {
/// Creates a new `DictionaryLiteral` instance from the given dictionary
public struct KeyValuePairs<Key, Value> : ExpressibleByDictionaryLiteral {
/// Creates a new `KeyValuePairs` instance from the given dictionary
/// literal.
///
/// The order of the key-value pairs is kept intact in the resulting
/// `DictionaryLiteral` instance.
/// `KeyValuePairs` instance.
public init(dictionaryLiteral elements: (Key, Value)...) {
self._elements = elements
}
@usableFromInline // FIXME(sil-serialize-all)
internal let _elements: [(Key, Value)]
}

/// `Collection` conformance that allows `DictionaryLiteral` to
@available(swift, deprecated: 5.0, renamed: "KeyValuePairs")
public typealias DictionaryLiteral<Key, Value> = KeyValuePairs<Key, Value>

/// `Collection` conformance that allows `KeyValuePairs` to
/// interoperate with the rest of the standard library.
extension DictionaryLiteral : RandomAccessCollection {
extension KeyValuePairs : RandomAccessCollection {
public typealias Indices = Range<Int>

/// The position of the first element in a nonempty collection.
///
/// If the `DictionaryLiteral` instance is empty, `startIndex` is equal to
/// If the `KeyValuePairs` instance is empty, `startIndex` is equal to
/// `endIndex`.
@inlinable // FIXME(sil-serialize-all)
public var startIndex: Int { return 0 }

/// The collection's "past the end" position---that is, the position one
/// greater than the last valid subscript argument.
///
/// If the `DictionaryLiteral` instance is empty, `endIndex` is equal to
/// If the `KeyValuePairs` instance is empty, `endIndex` is equal to
/// `startIndex`.
@inlinable // FIXME(sil-serialize-all)
public var endIndex: Int { return _elements.endIndex }

// FIXME(ABI)#174 (Type checker): a typealias is needed to prevent <rdar://20248032>
/// The element type of a `DictionaryLiteral`: a tuple containing an
/// The element type of a `KeyValuePairs`: a tuple containing an
/// individual key-value pair.
public typealias Element = (key: Key, value: Value)

Expand Down
1 change: 1 addition & 0 deletions test/api-digester/source-stability.swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Func Dictionary.filter(_:obsoletedInSwift4:) has been renamed to Func Dictionary
Func Set.filter(_:obsoletedInSwift4:) has been renamed to Func Set.filter(_:)

/* Type Changes */
Constructor Mirror.init(_:children:displayStyle:ancestorRepresentation:) has parameter 1 type change from DictionaryLiteral<String, Any> to KeyValuePairs<String, Any>
Constructor String.init(_:) has return type change from String? to String
Func Dictionary.filter(_:obsoletedInSwift4:) has return type change from [Dictionary<Key, Value>.Element] to [Dictionary<Key, Value>.Key : Dictionary<Key, Value>.Value]
Func Dictionary.makeIterator() has return type change from DictionaryIterator<Dictionary<Key, Value>.Key, Dictionary<Key, Value>.Value> to Dictionary<Key, Value>.Iterator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===--- DictionaryLiteral.swift ------------------------------------------===//
//===--- KeyValuePairs.swift ------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
Expand All @@ -21,14 +21,14 @@ import StdlibUnittest
// Check that the generic parameters are called 'Key' and 'Value'.
protocol TestProtocol1 {}

extension DictionaryLiteral where Key : TestProtocol1, Value : TestProtocol1 {
extension KeyValuePairs where Key : TestProtocol1, Value : TestProtocol1 {
var _keyAndValueAreTestProtocol1: Bool {
fatalError("not implemented")
}
}

func checkAssociatedTypes() {
typealias Subject = DictionaryLiteral<MinimalHashableValue, OpaqueValue<Int>>
typealias Subject = KeyValuePairs<MinimalHashableValue, OpaqueValue<Int>>
expectRandomAccessCollectionAssociatedTypes(
collectionType: Subject.self,
iteratorType: IndexingIterator<Subject>.self,
Expand All @@ -37,17 +37,17 @@ func checkAssociatedTypes() {
indicesType: CountableRange<Int>.self)
}

var strings: DictionaryLiteral = ["a": "1", "b": "Foo"]
expectType(DictionaryLiteral<String, String>.self, &strings)
var strings: KeyValuePairs = ["a": "1", "b": "Foo"]
expectType(KeyValuePairs<String, String>.self, &strings)

var stringNSStringLiteral: DictionaryLiteral = [
var stringNSStringLiteral: KeyValuePairs = [
"a": "1", "b": "Foo" as NSString]
expectType(DictionaryLiteral<String, NSString>.self, &stringNSStringLiteral)
expectType(KeyValuePairs<String, NSString>.self, &stringNSStringLiteral)

let aString = "1"
let anNSString = "Foo" as NSString
var stringNSStringLet: DictionaryLiteral = [ "a": aString as NSString, "b": anNSString]
expectType(DictionaryLiteral<String, NSString>.self, &stringNSStringLet)
var stringNSStringLet: KeyValuePairs = [ "a": aString as NSString, "b": anNSString]
expectType(KeyValuePairs<String, NSString>.self, &stringNSStringLet)

var hetero: DictionaryLiteral = ["a": 1 as NSNumber, "b": "Foo" as NSString]
expectType(DictionaryLiteral<String, NSObject>.self, &hetero)
var hetero: KeyValuePairs = ["a": 1 as NSNumber, "b": "Foo" as NSString]
expectType(KeyValuePairs<String, NSObject>.self, &hetero)