-
Notifications
You must be signed in to change notification settings - Fork 451
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
Use of 'self' in enum case adds trailing underscore instead of using backticks #1247
Comments
Backticks only help to avoid collisions with keywords, they don't help other kinds of collisions. For example, if someone names a field You can see more details about the possible collisions and how we handle them here: If you want to experiment with alternate approaches, please take a close look at how we test this point: |
I'd love to know why the decision was made to do this universally, rather than use backticks for conflicting keywords. Is it because of the amount of effort or maintainability cost needed to split the two? I'd be happy to take a look if folks don't feel strongly about the decision. |
I think part of this might go back to the pre Swift 3 days, when exactly what could/couldn't be handled via backticks wasn't as well defined.
|
I'm willing to consider such a change for 2.0. I have slight misgivings about breaking existing code more than necessary, but this should affect very few people. And as Thomas pointed out, now that the Swift syntax rules have settled down, maintenance cost is less of a concern. |
Note: Our testing for this uses the Swift Protobuf source code itself as a source of "words" that we need to handle. Any Swift keywords that aren't used in Swift Protobuf (e.g., concurrency-related stuff) may need additional testing. |
Cycling back to this, @tbkka's comment about keywords seems still somewhat relevant. Using a playground: import Foundation
public enum Foo : RawRepresentable {
public typealias RawValue = Int
case `self` // = 0
case UNRECOGNIZED(Int)
public init() {
self = .self
}
public init?(rawValue: Int) {
switch rawValue {
case 0: self = .self
default: self = .UNRECOGNIZED(rawValue)
}
}
public var rawValue: Int {
switch self {
case .self: return 0
case .UNRECOGNIZED(let i): return i
}
}
}
let a = Foo.self
let b = Foo.`self`
let c: Foo = .self
let d = Foo()
let e = Foo(rawValue: 0)
let f = Foo(rawValue: 1) So, yes, we could skip the underscores, but notice this snippet: let a = Foo.self
let b = Foo.`self`
let c: Foo = .self
|
Before opening a new issue, please double check the past issues (both open and
closed ones) to see if your problem has been discussed before and already
contains an answer/solution. Likewise, check the FAQ in the Documentation folder
for some common things -
https://github.com/apple/swift-protobuf/blob/main/Documentation/FAQ.md
Please be sure to include:
Cannot disclose
Cannot disclose
Cannot disclose
1.19.0
No compile errors
Proto
Swift
Is there a reason that
self
when used in enums creates a trailing underscore instead of using backticks? I've never seen this kind of naming convention in the swift language and I believe it's more commonplace to use backticks when there are collisions in keywords (e.g`default`
as another example.)The text was updated successfully, but these errors were encountered: