Navigation Menu

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-6323] Ability to verify enum associated values #48873

Open
NachoSoto opened this issue Nov 7, 2017 · 1 comment
Open

[SR-6323] Ability to verify enum associated values #48873

NachoSoto opened this issue Nov 7, 2017 · 1 comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal

Comments

@NachoSoto
Copy link
Contributor

Previous ID SR-6323
Radar None
Original Reporter @NachoSoto
Type New Feature
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels New Feature, LanguageFeatureRequest
Assignee None
Priority Medium

md5: 0142b41cc37c5399ac8957c39ecd5d29

Issue Description:

With a struct, one can override the default constructor to verify the data meets certain preconditions:

struct Content<T> {
  let list: [T]
  
  init(list: [T]) {
    precondition(!list.isEmpty)

    self.list = list
  }
}

However, the equivalent is not currently possible with enums:

enum Content<T> {
  case loading
  case loaded([T])
}

I'm not sure what the syntax for this would be, but I'd like to be able to detect an invalid definition of this enum at "creation time", rather than having these preconditions in a method with Content as a parameter.

@belkadan
Copy link
Contributor

belkadan commented Nov 7, 2017

The best design I've thought of in this space is marking enum cases private(init), like private(set), and then providing initializers or static functions that do "the same" thing.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal and removed new feature labels Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal
Projects
None yet
Development

No branches or pull requests

3 participants