6 years ago I had a pitch that went to proposal, but that just didn't fit with Swift 4 / ABI stability. I'd love to revisit it now.
Instead of having lots of computed properties using switch
, allow each case to declare its property value in "enum case blocks". This shuffles the data, pulling them all into the same place, making the entire file simpler to understand. There are some edge cases, so you'll want to read the original proposal. Here's the PR:
And here's the formatted view of the proposal text:
And the final closing comment on the ticket is this:
"The core team discussed this proposal and determined that it doesn't fit in the scope of Swift 4: it is purely additive (and could be introduced at any time), is not source-breaking (so it doesn't need to be front-loaded), and does not affect ABI or the standard library."
So I wonder if it's good to bring up again. What thoughts does everyone have after a lot of time using Swift?
The original discussions (email, not forum):
Week 1: The swift-evolution The Week Of Monday 2 January 2017 Archive by thread
Week 2: The swift-evolution The Week Of Monday 9 January 2017 Archive by thread
Newer Example
Here's a Moya enum in the new syntax. See the proposal text "mixed use" for placing common properties outside case blocks:
enum MarketingApi: TargetType {
var baseURL: URL {
URL(string: "https://api.example.com")!
}
case getMarketingitems(company: String, audience: String, region: String) {
var method: Moya.Method { .get }
var path: String { "v2/marketing/items" }
var headers: [String: String]? {[
"channel": "ios",
"x-request-id": UUID().uuidString,
"x-region": region
]}
var task: Task {
.requestParameters(parameters: [
"company": company,
"audience": audience
], encoding: JSONEncoding.default)
}
}
case getMarketingContentitems(company: String, currentDeviceRegion: String = "") {
var method: Moya.Method { .get }
var path: String { "v2/marketing/content-items" }
var headers: [String: String]? {[
"channel": "ios",
"x-request-id": UUID().uuidString,
"x-region": currentDeviceRegion
]}
var task: Task {
.requestParameters(parameters: [
"company": company,
], encoding: JSONEncoding.default)
}
}
}
Original
enum MarketingApi {
case getMarketingitems(company: String, audience: String, region: String)
case getMarketingContentitems(company: String, currentDeviceRegion: String = "")
}
extension MarketingApi: TargetType {
var baseURL: URL {
URL(string: "https://api.example.com")!
}
var path: String {
switch self {
case .getMarketingitems:
return "v2/marketing/items"
case .getMarketingContentitems:
return "v2/marketing/content-items"
}
}
var headers: [String: String]? {
switch self {
case let .getMarketingitems(company, audience, region):
return [
"channel": "ios",
"x-request-id": UUID().uuidString,
"x-region": region
]
case let .getMarketingContentitems(company, currentDeviceRegion):
return [
"channel": "ios",
"x-request-id": UUID().uuidString,
"x-region": currentDeviceRegion
]
}
}
var task: Task {
switch self {
case let .getMarketingitems(company, audience, region):
return .requestParameters(parameters: [
"company": company,
"audience": audience
], encoding: JSONEncoding.default)
case let .getMarketingContentitems(company, currentDeviceRegion):
return .requestParameters(parameters: [
"company": company
], encoding: JSONEncoding.default)
}
}
var method: Moya.Method {
switch self {
case .getMarketingitems:
return .get
case .getMarketingContentitems:
return .get
}
}
}