Skip to content
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

[SR-7239] Optional(nil) ? #49787

Closed
swift-ci opened this issue Mar 20, 2018 · 2 comments
Closed

[SR-7239] Optional(nil) ? #49787

swift-ci opened this issue Mar 20, 2018 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-7239
Radar None
Original Reporter mshibanami (JIRA User)
Type Bug
Status Resolved
Resolution Invalid

Attachment: Download

Environment

Apple Swift version 4.1 (swiftlang-902.0.43 clang-902.0.37.1)
Target: x86_64-apple-darwin17.4.0

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 15dc48b497ed49c70364a0b2f8640840

Issue Description:

See this code:

extension Array {
    func func1() {
        var optionals: [Element?] = self
        optionals[0] = self[0]
        print(optionals)

        let nonnulls
            = optionals.compactMap { $0 }
    }
}
[nil,1,2].func1()
  • I thought the nonnulls will be [1,2]. but actual value was [nil, 1, 2]. compactMap() didn't remove the nil.

  • print(optionals) outputs "[Optional(nil), Optional(Optional(1)), Optional(Optional(2))]"

Is this the expected result?
To be honest, I'm not sure this is a bug or a specification.

@belkadan
Copy link
Contributor

This is correct behavior; it's okay for Optionals to contain other Optionals, and so when you run this function you end up with [Int??], not just [Int?]. Then compactMap takes out all the top-level nil values, but not Optional(nil).

@swift-ci
Copy link
Collaborator Author

Comment by Manabu Nakazawa (JIRA)

Oh, I see. I didn't know about `??`.

This was a newbie issue. Sorry.

And thank you for letting me know!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants