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-10705] Enum Type Inference produces unexpected result with optional #53102

Closed
swift-ci opened this issue May 17, 2019 · 5 comments
Closed
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-10705
Radar None
Original Reporter ConfusedVorlon (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode Version 10.2.1 (10E1001)

Swift 5.0

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

md5: e393628d2dd2766b05c692488813f144

duplicates:

  • SR-2176 Add warning for ambiguous enum value assignment

Issue Description:

var s1:UITableViewCell.SelectionStyle?{{ = .none}}

expected:

s1 == UITableViewCell.SelectionStyle.none

actual:

s1 == Optional.none == nil

This just caught me out in a project, and it seems like confusing behaviour. I understand that it will generate a warning in swift 5.1

quoting the Swift guide:

The type of directionToHead is inferred when it's initialized with one of the possible values of CompassPoint. Once directionToHead is declared as a CompassPoint, you can set it to a different CompassPoint value using a shorter dot syntax:

wheras perhaps it should have a more complicated

... you can set it to a different CompassPoint value using a shorter dot syntax - except watch out for variables that are optional, and where the case is called .none, because that will clash with the Optional (which is actually implemented as an enum) and select Optional.none from the outer type

my feeling is that this is the kind of 'gotcha' which languages should avoid. If you have to have a deep understanding of how the language is implemented to understand a special case when assigning a variable - then that is suboptimal

possible ideas for fixes:

1) change the precedence

when assigning to

var s1:UITableViewCell.SelectionStyle?

it feels to me that s1 is 'more strongly' identified UITableViewCell.SelectionStyle rather than Optional.

this may have broader unpleasant consequences though

2) change the case name within Optional so that it clashes less often

perhaps Optional.none -> Optional.nil would seem nice (although nil is not currently allowed as a case name)

otherwise Optional.null, Optional.nothing, Optional.empty ???

@swift-ci
Copy link
Collaborator Author

Comment by Rob Jonson (JIRA)

just a thought as to intent. I can't imagine many developers who wanted to achieve

s1 = nil

who would write that as

s1 = .none

or to flip it; If you're writing .none, you almost certainly don't mean nil

@swift-ci
Copy link
Collaborator Author

Comment by Rob Jonson (JIRA)

another possible fix; Make .none a protected word which only Optional is allowed to use as an enum case (and rename it across the frameworks)

@theblixguy
Copy link
Collaborator

The only time I have seen `.none` being explicitly used (instead of `nil`) is when looking at some extensions on Optional in the stdlib. I don't think anyone really uses it explicitly, so it might not be a source-breaking change to look through the optional when using the dot syntax. We can then tweak any code in stdlib that assigns `.none` explicitly and assign `nil` instead.

enum Foo { case none }
enum Bar { case notNone }

let foo1: Foo? = .none // Foo.none
let foo2: Foo? = Optional.none // nil
let foo3: Foo = .none // Foo.none

let bar1: Bar? = .none // nil
let bar2: Bar? = Optional.none // nil

What do you think @belkadan @slavapestov?

@theblixguy
Copy link
Collaborator

> another possible fix; Make .none a protected word which only Optional is allowed to use as an enum case (and rename it across the frameworks)

That will definitely be a source breaking change and would involve a lot more work - I'd prefer we don't do this!

@belkadan
Copy link
Contributor

There's going to be a warning for this in Swift 5.1. I think that's probably the right place to stop.

@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