<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="">Thanx Maximillian for writing this, saved me from doing it and well put! :-)<div class=""><br class=""></div><div class="">- Paul<br class=""><div><blockquote type="cite" class=""><div class="">On Mar 23, 2016, at 5:46 AM, Maximilian Hünenberger 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=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><span class=""></span></div><div class=""><div class=""></div><div class="">Hi Brent,</div><div class=""><br class=""></div><div class="">First of all, I like your suggestions however there was already a proposal about "Pattern matching partial functions" which could address this and many other issues (See pull request #111; unfortunately the discussion doesn't went on... Hopefully this time).</div><div class=""><br class=""></div><div class="">This proposal addresses the issue of switch statements which return something in every "case". It uses special closures as unapplied switch statements and uses a global "match" function:</div><div class=""><br class=""></div><div class="">func match<T, U>(value: T, closure: T -> U) -> U {</div><div class=""> return closure(value)</div><div class="">}</div><div class=""><br class=""></div><div class="">The case you describe could now be rewritten to:</div><div class=""><br class=""></div><div class="">// no returns</div><div class=""><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">enum Suit: Int {<br class=""> case Hearts, Spades, Diamonds, Clubs<br class=""><br class=""> static var all: Suit[] { return [ Hearts, Spades, Diamonds, Clubs ] }<br class=""><br class=""> var description: String {<br class=""> return match(self) {<br class=""> case .Hearts: "♥️"<br class=""> case .Spades: "♠️"</span></font></div><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""> case .Diamonds: "♦️"<br class=""> case .Clubs: "♣️"<br class=""> }<br class=""> }<br class=""><br class=""> var isRed: Bool {<br class=""> return match(self) {<br class=""> case .Hearts, .Diamonds: true<br class=""> case .Spades, .Clubs: false<br class=""> }<br class=""> }<br class=""> }</span></font></div></div><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></font></div><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">// or even without case (which was highly controversial in the discussions)</span></font></div><div class=""><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">enum Suit: Int {<br class=""> case Hearts, Spades, Diamonds, Clubs<br class=""><br class=""> static var all: Suit[] { return [ Hearts, Spades, Diamonds, Clubs ] }<br class=""><br class=""> var description: String {<br class=""> return match(self) {<br class=""> cases .Hearts: "♥️", .Spades: "♠️", </span><span style="background-color: rgba(255, 255, 255, 0);" class="">.Diamonds: "♦️",</span><span style="background-color: rgba(255, 255, 255, 0);" class=""> .Clubs: "♣️"</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""> }<br class=""> }<br class=""><br class=""> var isRed: Bool {<br class=""> return match(self) {<br class=""> cases .Hearts, .Diamonds: true, .Spades, .Clubs: false<br class=""> }<br class=""> }<br class=""> }</span></div></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">What do you think about this?</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="">Kind regards</div><div class="">- Maximilian</div><div class=""><br class="">Am 23.03.2016 um 11:13 schrieb Brent Royal-Gordon via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>>:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><span class="">If you've written enums before, you've no doubt noticed the irritating phenomenon of `switch self` being absolutely everywhere. I first discovered this in some of my very first Swift code, code so old we were still using the `T[]` shorthand syntax:</span><br class=""><span class=""></span><br class=""><span class=""> enum Suit: Int {</span><br class=""><span class=""> case Hearts, Spades, Diamonds, Clubs</span><br class=""><span class=""></span><br class=""><span class=""> static var all: Suit[] { return [ Hearts, Spades, Diamonds, Clubs ] }</span><br class=""><span class=""></span><br class=""><span class=""> var description: String {</span><br class=""><span class=""> switch(self) {</span><br class=""><span class=""> case .Hearts:</span><br class=""><span class=""> return "♥️"</span><br class=""><span class=""> case .Spades:</span><br class=""><span class=""> return "♠️"</span><br class=""><span class=""> case .Diamonds:</span><br class=""><span class=""> return "♦️"</span><br class=""><span class=""> case .Clubs:</span><br class=""><span class=""> return "♣️"</span><br class=""><span class=""> }</span><br class=""><span class=""> }</span><br class=""><span class=""></span><br class=""><span class=""> var isRed: Bool {</span><br class=""><span class=""> switch(self) {</span><br class=""><span class=""> case .Hearts, .Diamonds:</span><br class=""><span class=""> return true</span><br class=""><span class=""> case .Spades, .Clubs:</span><br class=""><span class=""> return false</span><br class=""><span class=""> }</span><br class=""><span class=""> }</span><br class=""><span class=""> }</span><br class=""><span class=""></span><br class=""><span class="">It would be nice if we could somehow eliminate that. I have two suggestions:</span><br class=""><span class=""></span><br class=""><span class="">* Implicitly switch on `self` at the top level of a function or accessor (or at least an enum one with top-level `case` statements).</span><br class=""><span class=""></span><br class=""><span class=""> enum Suit: Int {</span><br class=""><span class=""> case Hearts, Spades, Diamonds, Clubs</span><br class=""><span class=""></span><br class=""><span class=""> static var all = [ Hearts, Spades, Diamonds, Clubs ]</span><br class=""><span class=""></span><br class=""><span class=""> var description: String {</span><br class=""><span class=""> case .Hearts:</span><br class=""><span class=""> return "♥️"</span><br class=""><span class=""> case .Spades:</span><br class=""><span class=""> return "♠️"</span><br class=""><span class=""> case .Diamonds:</span><br class=""><span class=""> return "♦️"</span><br class=""><span class=""> case .Clubs:</span><br class=""><span class=""> return "♣️"</span><br class=""><span class=""> }</span><br class=""><span class=""></span><br class=""><span class=""> var isRed: Bool {</span><br class=""><span class=""> case .Hearts, .Diamonds:</span><br class=""><span class=""> return true</span><br class=""><span class=""> case .Spades, .Clubs:</span><br class=""><span class=""> return false</span><br class=""><span class=""> }</span><br class=""><span class=""> }</span><br class=""><span class=""></span><br class=""><span class="">* Allow you to attach member definitions to particular cases. It would be an error if they didn't all define the same members, unless there was a top-level catchall.</span><br class=""><span class=""></span><br class=""><span class=""> enum Suit: Int {</span><br class=""><span class=""> var isRed: Bool { return false }</span><br class=""><span class=""></span><br class=""><span class=""> case Hearts {</span><br class=""><span class=""> let description: String { return "♥️" }</span><br class=""><span class=""> let isRed: Bool { return true }</span><br class=""><span class=""> }</span><br class=""><span class=""> case Spades {</span><br class=""><span class=""> let description: String { return "♠️" }</span><br class=""><span class=""> }</span><br class=""><span class=""> case Diamonds {</span><br class=""><span class=""> let description: String { return "♦️" }</span><br class=""><span class=""> let isRed: Bool { return true }</span><br class=""><span class=""> }</span><br class=""><span class=""> case Clubs {</span><br class=""><span class=""> let description: String { return "♣️" }</span><br class=""><span class=""> }</span><br class=""><span class=""></span><br class=""><span class=""> static var all = [ Hearts, Spades, Diamonds, Clubs ]</span><br class=""><span class=""> }</span><br class=""></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">Any thoughts? This has, to be honest, bothered me since approximately the third day I used the language; I'd love to address it sooner or later.</span><br class=""><span class=""></span><br class=""><span class="">-- </span><br class=""><span class="">Brent Royal-Gordon</span><br class=""><span class="">Architechies</span><br class=""><span class=""></span><br class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></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=""></div></body></html>