<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">You should be able to do this today using "let x as Y":</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div dir="ltr" class=""><div class=""><div class="">protocol Serializable {</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> func serialize() -> String?</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class="">}</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""><br class=""></div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class="">struct CreateFooRequest: Serializable {</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> let title: String</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> func serialize() -> String? { return nil }</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class="">}</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""><br class=""></div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class="">struct UpdateFooRequest: Serializable {</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> func serialize() -> String? { return nil}</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class="">}</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""><br class=""></div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class="">enum APIRequest {</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> case CreateFoo(request: CreateFooRequest)</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> case UpdateFoo(request: UpdateFooRequest)</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> //...</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""><br class=""></div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> var HTTPBody: String? {</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> switch self {</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> case let .CreateFoo(request as Serializable),</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> let .UpdateFoo(request as Serializable): </div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> return request.serialize()</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> }</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class=""> }</div></div></div></div><div class=""><div dir="ltr" class=""><div class=""><div class="">}</div></div></div></div></blockquote><div class=""><div dir="ltr" class=""><div class=""><div class=""><br class=""></div><div class="">I don't think we want to automatically try to infer a common type—it's too easy for the compiler to pick a surprising type that way—but it's there when you need it.</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></div><div><blockquote type="cite" class=""><div class="">On Mar 15, 2016, at 14:17 , Andrii Chernenko via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Hello everyone,</div><div class=""><br class=""></div>This is a great proposal and indeed it would be nice to have this feature in Swift.<div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">I'll illustrate with an example. Suppose I am implementing REST API requests using enums.</div><div class=""><br class=""></div><div class="">protocol Serializable {</div><div class=""> func serialize() -> NSData</div><div class="">}</div><div class=""><br class=""></div><div class="">struct CreateFooRequest: Serializable {</div><div class=""> let title: String</div><div class=""> let date: NSDate</div><div class="">}</div><div class=""><br class=""></div><div class=""><div class="">struct UpdateFooRequest: Serializable {}</div></div><div class=""><br class=""></div><div class="">...</div><div class=""><br class=""></div><div class="">enum APIRequest {</div><div class=""> case .CreateFoo(request: CreateFooRequest),</div><div class=""><div class=""> case .UpdateFoo(request: UpdateFooRequest)</div></div><div class=""> ...</div><div class=""><br class=""></div><div class=""><div class=""> var HTTPRequest: NSHTTPRequest { ... }</div></div><div class=""><br class=""></div><div class=""> var URL: NSURL { ... }</div><div class=""><br class=""></div><div class=""> var headers: [String: String] { ... }</div><div class=""><br class=""></div><div class=""> var HTTPBody: NSData? {</div><div class=""> switch self {</div><div class=""> case let .CreateFoo(request): </div><div class=""> return request.serialize()</div><div class=""> case let .UpdateFoo(request): </div><div class=""> return request.serialize()</div><div class=""> ...</div><div class=""> }<br class=""></div><div class=""> }</div><div class="">}<br class=""></div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">Just in case, here's the link to the proposal: <a href="https://github.com/apple/swift-evolution/pull/119" class="">https://github.com/apple/swift-evolution/pull/119</a></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>