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-887] Expose the declaration order of Enum cases with payloads #43499

Open
masters3d opened this issue Mar 6, 2016 · 8 comments
Open

[SR-887] Expose the declaration order of Enum cases with payloads #43499

masters3d opened this issue Mar 6, 2016 · 8 comments
Labels
compiler The Swift compiler in itself feature A feature request or implementation improvement

Comments

@masters3d
Copy link
Contributor

Previous ID SR-887
Radar None
Original Reporter @masters3d
Type Improvement
Status Reopened
Resolution
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, LanguageFeatureRequest
Assignee None
Priority Medium

md5: 8985ba38c2b176524ace48d4f576b55c

Issue Description:

error:
"Enum with raw type cannot have cases with arguments"

In addition to being able to carry a payload, it would be really useful if enums could also have a way to expose their order as an Int or some sort of Index Raw type so they can be ranked by importance.

BEFORE
```
enum HandRank{
    case highCard(PlayingCard)
    case onePair(Rank,card1:Rank, card2:Rank, card3:Rank )
    case twoPair(high:Rank,low:Rank, highCard:PlayingCard)
    case threeOfAKind(three:Rank)
    case straight(high:Rank)
    case flush(Rank, Suit)
    case fullHouse(three:Rank)
    case fourOfAKind(four:Rank)
    case straightFlush(Rank,Suit)
    
    func order()->Int{
        switch self {
        case .highCard(_): return 1
        case .onePair(_, card1: _, card2: _, card3: _): return 2
        case .twoPair(high: _, low: _, highCard: _): return 3
        case threeOfAKind(three:_): return 4
        case straight(high:_): return 5
        case flush(_, _): return 6
        case fullHouse(three:_): return 7
        case fourOfAKind(four:_): return 8
        case straightFlush(_,_): return 9
        }
    }
.........
}

```

AFTER:

```
enum HandRank:Int{
    case highCard(PlayingCard) = 1
    case onePair(Rank,card1:Rank, card2:Rank, card3:Rank )
    case twoPair(high:Rank,low:Rank, highCard:PlayingCard)
    case threeOfAKind(three:Rank)
    case straight(high:Rank)
    case flush(Rank, Suit)
    case fullHouse(three:Rank)
    case fourOfAKind(four:Rank)
    case straightFlush(Rank,Suit)
.....
}
```
@lilyball
Copy link
Mannequin

lilyball mannequin commented Mar 7, 2016

If an enum has a raw type, it doesn't just have a rawValue property that contains the raw value, it also has to respond to init?(rawValue: T) to be created with the raw value. An enum with associated values obviously cannot be created from a raw value, because the associated values would not have a definition.

@masters3d
Copy link
Contributor Author

I guess it doesn't have to be a raw value. I was just trying to figure out a way to get the order of the cases with payloads with out having to create a helper function.

@lilyball
Copy link
Mannequin

lilyball mannequin commented Mar 7, 2016

Sounds like what you want is some function that takes any enum value and gives you the discriminant, similar to Rust's std::intrinsics::discriminant_value. This would be an API addition and would need to go through swift-evolution.

@belkadan
Copy link
Contributor

belkadan commented Mar 8, 2016

"discriminant" would be the wrong name here because (a) Swift doesn't guarantee to use unique discriminants if it can infer the info some other way, and (b) the order Swift allocates discriminants (deliberately) isn't guaranteed to match the order of declarations. (And with normal enums-with-raw-types you can manually set the raw values, but it sounds like you're okay with not doing that.)

I don't think we have anything today that makes sense to expose at the user level, hence the LanguageFeatureRequest tag. This would need to go through the Swift Evolution Process, and I wouldn't call it "raw values" since it's not round-trippable.

@masters3d
Copy link
Contributor Author

Sounds good. Thanks for the input Kevin and Jordan. I am going to wait a little bit until closer to swift 3-4. There are other things like getting all the cases that also would be nice to get for free.

@masters3d
Copy link
Contributor Author

One of these proposals may be a requirement for this SR
apple/swift-evolution#199
OR
apple/swift-evolution#114

@masters3d
Copy link
Contributor Author

allCases solved this for me.

@lilyball
Copy link
Mannequin

lilyball mannequin commented Jun 27, 2018

allCases is not a sufficient solution, for two reasons:

1. That requires an O(N) search to find the index of a given case
2. More importantly, allCases doesn't work for enums with payloads, and this ticket is specifically about enums with payloads.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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 improvement
Projects
None yet
Development

No branches or pull requests

2 participants