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-30] Enumerations could have a "allValues" property #42653

Closed
swift-ci opened this issue Dec 4, 2015 · 4 comments
Closed

[SR-30] Enumerations could have a "allValues" property #42653

swift-ci opened this issue Dec 4, 2015 · 4 comments
Assignees
Labels
compiler The Swift compiler in itself enum Feature → type declarations: Swift enumeration declarations feature A feature request or implementation improvement swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 4, 2015

Previous ID SR-30
Radar rdar://problem/17102392
Original Reporter RuiCarneiro (JIRA User)
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 6
Component/s
Labels Improvement, LanguageFeatureRequest
Assignee @CodaFi
Priority Medium

md5: 6f9747e0e0648c9bf5fee84b0f66cd7f

relates to:

  • SR-3050 enum cases should be enumerable

Issue Description:

Sometimes, it's convenient to have an allValues property for an enum that automatically generates a collection with all possible elements.

eg:

{{enum Fruits { case Apple, Orange, Peach, Banana }

for fruit in Fruits.allValues {
// ...
}}}

{color:red}On another note, it would be handy to have a reverse function for the rawValue propriety

eg:

{{enum Fruits : Int { case Apple = 0, Orange ... }

let fruit : Fruits? = Fruits.reverseRawValue(1)

// fruit == Fruits.Orange}}{color}

Thanks, Kevin Ballard.

@lilyball
Copy link
Mannequin

lilyball mannequin commented Dec 4, 2015

We already have a reverse function for the rawValue property; every RawRepresentable type gets a failable initializer init?(rawValue: Self.RawValue).

As for {{allValues}, I'd like to point out that this could only be done for C-like enums (enums without associated values). It's also not obvious how it should work; does it return an Array or some lazy generator? If it returns an Array, does it generate one on every call, or does it save it as a static value? Generating it on every call has performance implications, but saving it as a static value takes memory. Furthermore, once you have an allValues list, the next natural thing to want is some way to get the previous/next variant from a given variant, but does it actually make sense to do that? Also, making any change like this means automatically adding a member to all C-like enums even when they don't declare any protocol conformance, which may be undesired (maybe I want to have an allValues member that returns something else, e.g. if I have variants in my enum that aren't truly "values" but are instead things like bitmasks if my enum is a set of bitflags).

It seems to me that swift-evolution is the right place to discuss the allValues suggestion.

@moiseev
Copy link
Mannequin

moiseev mannequin commented May 3, 2018

CodaFi (JIRA User), found this issue. Hasn't it been fixed recently?

@CodaFi
Copy link
Member

CodaFi commented May 3, 2018

Yep. Addressed by the merge of #13655.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself enum Feature → type declarations: Swift enumeration declarations swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented labels Mar 22, 2023
@AnthonyLatsis
Copy link
Collaborator

Swift evolution proposal: SE-0194.

This issue was closed.
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 enum Feature → type declarations: Swift enumeration declarations feature A feature request or implementation improvement swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented
Projects
None yet
Development

No branches or pull requests

3 participants