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-3055] Switch statements fail to test optionals for equality #45645

Closed
swift-ci opened this issue Oct 27, 2016 · 3 comments
Closed

[SR-3055] Switch statements fail to test optionals for equality #45645

swift-ci opened this issue Oct 27, 2016 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-3055
Radar None
Original Reporter bogidon (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 8.0
OS X 10.11.6

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

md5: 81813b398f05d32291bc44dcd426803c

relates to:

  • SR-1535 No == overload exists for comparing optional arrays.

Issue Description:

1. If the switch statement's "value to be considered" is an optional and is being equated to other optionals, Swift will bridge the "value to be considered" to a _OptionalNilComparisonType, which cannot be equated to an optional of the same type.

let pet: String? = "dog"
let dog: String? = "dog"
let cat: String? = nil

// Does not work
switch pet {
case dog:
    print(dog)
    
case cat:
    print(cat)
    
default:
    print("did not match")
}

// Does work
switch () {
case _ where pet == dog:
    print(dog)
    
case _ where pet == cat:
    print(cat)
    
default:
    print("did not match")
}

2. If the switch statement's "value to be considered" is not an optional but is being equated to optionals, the act of equating fails.

let pet: String = "dog"
let dog: String? = "dog"
let cat: String? = nil

// Does not work
switch pet {
case dog:
    print(dog)
    
case cat:
    print(cat)
    
default:
    print("did not match")
}

// Does work
switch () {
case _ where pet == dog:
    print(dog)
    
case _ where pet == cat:
    print(cat)
    
default:
    print("did not match")
}

This is my first time posting here, I'm sorry if the formatting is off.

@belkadan
Copy link
Contributor

The first problem should go away with conditional conformances, which will allow Optional to be Equatable when its element type is Equatable. The second one is correct behavior, I think, but at that point you'd be able to convert to the first by explicitly coercing to Optional in the argument to switch.

@Dante-Broggi
Copy link
Contributor

@belkadan, your comment implies something should happen to this report now that SE-0143 (Conditional Conformances) was implemented (in Swift 4.2)?

@belkadan
Copy link
Contributor

Yep, both examples now pass, so we can call this done.

@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. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants