Open bohdany-cricut opened 1 week ago
Thanks for taking the time to write this up in a PR!
This change looks like it’s only in an HTML <!-- -->
comment, which isn't part of the rendered book. The block that this PR changes is essentially a leftover from migration to DocC. The build system TSPL used before DocC tested the code listings. DocC doesn't support tested code listings, but we kept the tests during the export, so we would still have the hidden "expectation" lines if we ever try to resume code testing.
Looking at the rendered version of the content, the example uses this form:
var someInts: [Int] = []
However, as you noted, the text and the code don’t match: The text talks about type inference, but the code has an explicit type in it. This looks an oversight from 2021 in commit 639bcc61dde12b16cb50fcd81885292bfd6a2883 when cleaning up initializer syntax throughout the book. We should keep the code as-is, since it follows the code style guidelines for empty collections.
In this case, I think we might want to just remove the mention of type inference, since it would be pretty rare to create an empty array in code that has enough type information to infer the array’s type. If we want to mention something about that, one example is passing an empty array to a function.
I'll be out of the office next week for Thanksgiving — if you'd like to try rewriting, take a look at your changes when I get back. Otherwise, I'll push another commit to your branch after the holiday, changing the prose.
@amartini51 Thank you for taking the time to review my PR! I appreciate the context regarding the migration to DocC.
After carefully considering your feedback, I believe the best solution would be to update the example to use the actual initializer syntax as an alternative to the preferred array literal syntax. Here’s why I think this approach would address the concerns and improve the clarity of the guide:
1. Clarifying "Initializer Syntax" The section currently states:
You can create an empty array of a certain type using initializer syntax.
However, the example provided (var someInts: [Int] = []
) does not use an initializer. Instead, it assigns an empty array literal to a variable with an explicitly declared type. This is misleading because it does not demonstrate what "initializer syntax" means.
Updating the section to explicitly show both methods - the preferred array literal syntax ([]
) and the initializer syntax ([Int]()
) - provides clarity and ensures completeness. Additionally, it’s worth noting that the "Collection Types" page doesn’t currently include any examples showcasing the array initializer syntax.
2. Avoiding Redundancy
The "Alternatively" example, which follows the initializer syntax section, states:
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):
This is illustrated with:
someInts = []
However, this example demonstrates the same concept as the first example: assigning an empty array literal ([]
) to a variable. This creates redundancy without adding new information.
By explicitly stating that the array literal ([])
is the preferred method and introducing the initializer syntax ([Int]()
) as an alternative, the section would comprehensively cover both ways of creating an empty array without duplication.
This pull request resolves an inconsistency in the "Creating an Empty Array" section of the Swift Language Guide under "Collection Types."
Changes:
var someInts: [Int] = []
withvar someInts = [Int]()
to accurately demonstrate type inference, aligning with the accompanying explanatory text.Rationale:
The previous example did not demonstrate type inference, as the type
[Int]
was explicitly declared using: [Int]
. However, the accompanying text states:This was misleading because type inference did not occur in the provided example. The updated example clarifies the explanation and ensures consistency across the guide, aligning with similar sections such as "Creating and Initializing an Empty Set."