Skip to content

Commit

Permalink
Initialize empty collections using a literal.
Browse files Browse the repository at this point in the history
Searched for '\[.*\]()' which could technically match other things, but
only actually matched the places where I needed to change this.
  • Loading branch information
amartini51 committed Feb 2, 2021
1 parent 6192ee8 commit 639bcc6
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 26 deletions.
6 changes: 3 additions & 3 deletions GuidedTour/GuidedTour.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ use the initializer syntax.

.. testcode:: guided-tour

-> let emptyArray = [String]()
-> let emptyDictionary = [String: Float]()
-> let emptyArray: [String] = []
-> let emptyDictionary: [String: Float] = [:]

If type information can be inferred,
you can write an empty array as ``[]``
Expand Down Expand Up @@ -1378,7 +1378,7 @@ to make a generic function or type.
.. testcode:: guided-tour

-> func makeArray<Item>(repeating item: Item, numberOfTimes: Int) -> [Item] {
var result = [Item]()
var result: [Item] = []
for _ in 0..<numberOfTimes {
result.append(item)
}
Expand Down
5 changes: 1 addition & 4 deletions LanguageGuide/Closures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ For example:

.. testcode:: noescape-closure-as-argument, implicit-self-struct

-> var completionHandlers = [() -> Void]()
-> var completionHandlers: [() -> Void] = []
-> func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
completionHandlers.append(completionHandler)
}
Expand Down Expand Up @@ -953,9 +953,6 @@ The ``@escaping`` attribute is described above in :ref:`Closures_Noescape`.
<- Now serving Barry!
<- Now serving Daniella!

.. Explicit type annotations instead of [Foo]() constructor syntax to work around
<rdar://problem/25150801> Array constructor syntax - can't parse arrays of function type
In the code above,
instead of calling the closure passed to it
as its ``customerProvider`` argument,
Expand Down
4 changes: 2 additions & 2 deletions LanguageGuide/CollectionTypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ using initializer syntax:

.. testcode:: arraysEmpty

-> var someInts = [Int]()
-> var someInts: [Int] = []
-> print("someInts is of type [Int] with \(someInts.count) items.")
<- someInts is of type [Int] with 0 items.

Expand Down Expand Up @@ -788,7 +788,7 @@ you can create an empty ``Dictionary`` of a certain type by using initializer sy

.. testcode:: dictionariesEmpty

-> var namesOfIntegers = [Int: String]()
-> var namesOfIntegers: [Int: String] = [:]
// namesOfIntegers is an empty [Int: String] dictionary

This example creates an empty dictionary of type ``[Int: String]``
Expand Down
4 changes: 2 additions & 2 deletions LanguageGuide/ControlFlow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ For more about ranges, see :ref:`BasicOperators_RangeOperators`.
.. testcode:: forLoops

-> let minutes = 60
>> var result = [Int]()
>> var result: [Int] = []
-> for tickMark in 0..<minutes {
// render the tick mark each minute (60 times)
>> result.append(tickMark)
Expand All @@ -156,7 +156,7 @@ Use the ``stride(from:to:by:)`` function to skip the unwanted marks.
.. testcode:: forLoops

-> let minuteInterval = 5
>> result = [Int]()
>> result = []
-> for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
// render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
>> result.append(tickMark)
Expand Down
12 changes: 6 additions & 6 deletions LanguageGuide/Generics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ in this case for a stack of ``Int`` values:
.. testcode:: genericStack

-> struct IntStack {
var items = [Int]()
var items: [Int] = []
mutating func push(_ item: Int) {
items.append(item)
}
Expand Down Expand Up @@ -305,7 +305,7 @@ Here's a generic version of the same code:
.. testcode:: genericStack

-> struct Stack<Element> {
var items = [Element]()
var items: [Element] = []
mutating func push(_ item: Element) {
items.append(item)
}
Expand Down Expand Up @@ -682,7 +682,7 @@ adapted to conform to the ``Container`` protocol:

-> struct IntStack: Container {
// original IntStack implementation
var items = [Int]()
var items: [Int] = []
mutating func push(_ item: Int) {
items.append(item)
}
Expand Down Expand Up @@ -727,7 +727,7 @@ You can also make the generic ``Stack`` type conform to the ``Container`` protoc

-> struct Stack<Element>: Container {
// original Stack<Element> implementation
var items = [Element]()
var items: [Element] = []
mutating func push(_ item: Element) {
items.append(item)
}
Expand Down Expand Up @@ -1353,7 +1353,7 @@ For example:
-> extension Container {
subscript<Indices: Sequence>(indices: Indices) -> [Item]
where Indices.Iterator.Element == Int {
var result = [Item]()
var result: [Item] = []
for index in indices {
result.append(self[index])
}
Expand All @@ -1365,7 +1365,7 @@ For example:

>> struct IntStack: Container {
// original IntStack implementation
var items = [Int]()
var items: [Int] = []
mutating func push(_ item: Int) {
items.append(item)
}
Expand Down
2 changes: 1 addition & 1 deletion LanguageGuide/Initialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,7 @@ The ``boardColors`` array is initialized with a closure to set up its color valu

-> struct Chessboard {
let boardColors: [Bool] = {
var temporaryBoard = [Bool]()
var temporaryBoard: [Bool] = []
var isBlack = false
for i in 1...8 {
for j in 1...8 {
Expand Down
2 changes: 1 addition & 1 deletion LanguageGuide/OpaqueTypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ which you can use as the requirement for the ``Shape`` protocol:
-> struct Triangle: Shape {
var size: Int
func draw() -> String {
var result = [String]()
var result: [String] = []
for length in 1...size {
result.append(String(repeating: "*", count: length))
}
Expand Down
2 changes: 1 addition & 1 deletion LanguageGuide/OptionalChaining.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ which is initialized with an empty array of type ``[Room]``:
.. testcode:: optionalChaining

-> class Residence {
var rooms = [Room]()
var rooms: [Room] = []
var numberOfRooms: Int {
return rooms.count
}
Expand Down
2 changes: 1 addition & 1 deletion LanguageGuide/Properties.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ neither of which is shown in full:
---
-> class DataManager {
lazy var importer = DataImporter()
var data = [String]()
var data: [String] = []
// the DataManager class would provide data management functionality here
}
---
Expand Down
2 changes: 1 addition & 1 deletion LanguageGuide/TypeCasting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ The example creates an array called ``things``, which can store values of type `

.. testcode:: typeCasting, typeCasting-err

-> var things = [Any]()
-> var things: [Any] = []
---
-> things.append(0)
-> things.append(0.0)
Expand Down
4 changes: 0 additions & 4 deletions ReferenceManual/Expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,6 @@ pair of square brackets and can be used to create an empty array of a specified

-> var emptyArray: [Double] = []

.. Note: The normal style for the above would be
var emptyArray = [Double]()
but we're explicitly demonstrating the [] literal syntax here.
A :newTerm:`dictionary literal` is
an unordered collection of key-value pairs.
It has the following form:
Expand Down

0 comments on commit 639bcc6

Please sign in to comment.