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-37] RawRepresentable enum types should be auto unpacked. #42660
Comments
Respectfully, I disagree. Swift errs on the side of requiring explicit type conversions in most cases that aren't upcasts (the biggest exception being bridging Swift types to obj-c). I don't like treating the enum like it was a string. If you actually want them to be strings, you could say something like
Furthermore, this suggestion seems like something that should be posted to the swift-evolution list instead of here. |
Comment by Ben Lachman (JIRA) I believe this would be an acceptable heuristic as well, however it doesn't work either. It still forces the developer to append the unsightly and no more clear .rawValue onto every enum. Additionally, Swift has already moved in the direction of enum's as strings with the adherence to (I believe) CustomStringConvertible (assumedly mostly for ease of debugging via print() as in my example and the like). I think you're right that this might be more appropriate for swift-evolution… anyone else have an opinion? |
|
Yes, this would need to go through discussion on swift-evolution. |
Comment by Raphael (JIRA) I agree. Philosophical discourse aside, declaring `enum A: String` and then having to `rawValue` everywhere you want to use the single, constant string represented by a case of this enum is just silly. I think, though, that we should talk about auto-unpacking all RawRepresentables. |
Comment by Raphael (JIRA) Today, this related issue confused me: |
That's different enough to be worth a separate issue. I'm not sure that's at all "obvious", though, since the normal stringification of enum cases includes their names. |
Comment by Raphael (JIRA) Yup, that's why I put it in a comment here: if RawRepresentable enums would act like their raw type, it would make sense to life more than just hashValue. |
Additional Detail from JIRA
md5: 821bcb1fe2c1f28e6ac53d7cfb2e0ad6
relates to:
Issue Description:
In many cases it seems most natural to store application level constants in an enum. For instance:
As one would expect (and as of Swift 2) you can now
This is because of the addition of the CustomStringConvertable protocol. However in similar situations where one might want to pass an enum as it's raw representation, it fails:
This seems counter intuitive. Especially in the case of an enum with an explicitly typed raw value (e.g. enum Notifications: String). This has lead to a plethora of work arounds. Here are three:
In light of this, RawRepresentable types logically should map to their raw values when passed as arguments of that specified types, such that a simple
The text was updated successfully, but these errors were encountered: