[swift-evolution] Draft Proposal: Declare variables in 'case' labels with multiple patterns

Andrii Chernenko mail at andrii.ch
Tue Mar 15 16:17:31 CDT 2016

Hello everyone,

This is a great proposal and indeed it would be nice to have this feature
in Swift.

However, there is another use case I have in mind. It would be nice to
match multiple cases with associated values of different types which
conform to a certain protocol.

I'll illustrate with an example. Suppose I am implementing REST API
requests using enums.

protocol Serializable {
  func serialize() -> NSData

struct CreateFooRequest: Serializable {
  let title: String
  let date: NSDate

struct UpdateFooRequest: Serializable {}


enum APIRequest {
    case .CreateFoo(request: CreateFooRequest),
    case .UpdateFoo(request: UpdateFooRequest)

    var HTTPRequest: NSHTTPRequest { ... }

    var URL: NSURL { ... }

    var headers: [String: String] { ... }

    var HTTPBody: NSData? {
        switch self {
          case let .CreateFoo(request):
            return request.serialize()
          case let .UpdateFoo(request):
            return request.serialize()

Current proposal aims to eliminate repetitiveness such as in the HTTPBody
property of the APIRequest enum, but does it cover the case where
associated value types are different, but conform to the same protocol? It
is not clear to me.

Just in case, here's the link to the proposal:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160315/3a044b8a/attachment.html>

More information about the swift-evolution mailing list