swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.48k stars 10.35k forks source link

[SR-12623] Complete docs about reassignment to collection types in the language guide #55067

Open swift-ci opened 4 years ago

swift-ci commented 4 years ago
Previous ID SR-12623
Radar rdar://problem/62894498
Original Reporter fxn (JIRA User)
Type Improvement
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | | |Labels | Improvement | |Assignee | None | |Priority | Medium | md5: c7a570afae42c961a0d834e82f6cfe5f

Issue Description:

The section about collections in the language guide explains that if you want to reassign a variable that stores an array with an empty array, the compiler can infer the type:

Alternatively, if the context already provides type information,
such as a function argument or an already typed variable or constant,
you can create an empty array with an empty array literal, which is
written as [] (an empty pair of square brackets):

someInts.append(3)
// someInts now contains 1 value of type Int
someInts = []
// someInts is now an empty array, but is still of type [Int]

For completeness/symmetry, I believe it would be nice to say the equivalent for the other two types covered in that section: sets and dictionaries.

If this documentation was open source, I would be glad to contribute a patch myself, but I've been told it is not, so filling a bug.

Please, let me know if documentation suggestions should go somewhere else.

beccadax commented 4 years ago

@swift-ci create