-
Notifications
You must be signed in to change notification settings - Fork 171
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
Fix type inference example in "Creating an Empty Array" #341
base: main
Are you sure you want to change the base?
Fix type inference example in "Creating an Empty Array" #341
Conversation
Updated the array initialization example to demonstrate type inference (var someInts = [Int]()) for consistency with other sections in the guide, such as Sets and Dictionaries. This change aligns the code example with the accompanying explanatory text.
Thanks for taking the time to write this up in a PR! This change looks like it’s only in an HTML 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 639bcc6 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. |
- Highlight empty array literals (`[]`) as the preferred method. - Add initializer syntax (`[Type]()`) as an alternative. - Reorganize examples to reduce redundancy and improve clarity.
@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"
However, the example provided ( Updating the section to explicitly show both methods - the preferred array literal syntax ( 2. Avoiding Redundancy The "Alternatively" example, which follows the initializer syntax section, states:
This is illustrated with:
However, this example demonstrates the same concept as the first example: assigning an empty array literal ( By explicitly stating that the array literal ( |
These aren't used for anything today -- they're just an (intentional) leftover from RST migration, to ease a possible re-adoption of tested code listings some day.
I think pointing out [Int]() from the code example is easier to follow than [Element](), since the reader doesn't have to mentally map or substitute a placeholder. Reserve emphasis around new terms for when they're being introduced and also explicitly defined. No need to call out the book's style in the book itself.
Thanks for your revision and for your patience — I expected to come back to this PR sooner. I've pushed a few minor changes to this branch. This looks good to me now. |
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."