<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><div></div><div>Hi Brent,</div><div><br></div><div>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><br></div><div>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><br></div><div>func match<T, U>(value: T, closure: T -> U) -> U {</div><div> return closure(value)</div><div>}</div><div><br></div><div>The case you describe could now be rewritten to:</div><div><br></div><div>// no returns</div><div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">enum Suit: Int {<br> case Hearts, Spades, Diamonds, Clubs<br><br> static var all: Suit[] { return [ Hearts, Spades, Diamonds, Clubs ] }<br><br> var description: String {<br> return match(self) {<br> case .Hearts: "♥️"<br> case .Spades: "♠️"</span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> case .Diamonds: "♦️"<br> case .Clubs: "♣️"<br> }<br> }<br><br> var isRed: Bool {<br> return match(self) {<br> case .Hearts, .Diamonds: true<br> case .Spades, .Clubs: false<br> }<br> }<br> }</span></font></div></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">// or even without case (which was highly controversial in the discussions)</span></font></div><div><div><span style="background-color: rgba(255, 255, 255, 0);">enum Suit: Int {<br> case Hearts, Spades, Diamonds, Clubs<br><br> static var all: Suit[] { return [ Hearts, Spades, Diamonds, Clubs ] }<br><br> var description: String {<br> return match(self) {<br> cases .Hearts: "♥️", .Spades: "♠️", </span><span style="background-color: rgba(255, 255, 255, 0);">.Diamonds: "♦️",</span><span style="background-color: rgba(255, 255, 255, 0);"> .Clubs: "♣️"</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> }<br> }<br><br> var isRed: Bool {<br> return match(self) {<br> cases .Hearts, .Diamonds: true, .Spades, .Clubs: false<br> }<br> }<br> }</span></div></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">What do you think about this?</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div>Kind regards</div><div>- Maximilian</div><div><br>Am 23.03.2016 um 11:13 schrieb Brent Royal-Gordon via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div><span>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><span></span><br><span> enum Suit: Int {</span><br><span> case Hearts, Spades, Diamonds, Clubs</span><br><span></span><br><span> static var all: Suit[] { return [ Hearts, Spades, Diamonds, Clubs ] }</span><br><span></span><br><span> var description: String {</span><br><span> switch(self) {</span><br><span> case .Hearts:</span><br><span> return "♥️"</span><br><span> case .Spades:</span><br><span> return "♠️"</span><br><span> case .Diamonds:</span><br><span> return "♦️"</span><br><span> case .Clubs:</span><br><span> return "♣️"</span><br><span> }</span><br><span> }</span><br><span></span><br><span> var isRed: Bool {</span><br><span> switch(self) {</span><br><span> case .Hearts, .Diamonds:</span><br><span> return true</span><br><span> case .Spades, .Clubs:</span><br><span> return false</span><br><span> }</span><br><span> }</span><br><span> }</span><br><span></span><br><span>It would be nice if we could somehow eliminate that. I have two suggestions:</span><br><span></span><br><span>* 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><span></span><br><span> enum Suit: Int {</span><br><span> case Hearts, Spades, Diamonds, Clubs</span><br><span></span><br><span> static var all = [ Hearts, Spades, Diamonds, Clubs ]</span><br><span></span><br><span> var description: String {</span><br><span> case .Hearts:</span><br><span> return "♥️"</span><br><span> case .Spades:</span><br><span> return "♠️"</span><br><span> case .Diamonds:</span><br><span> return "♦️"</span><br><span> case .Clubs:</span><br><span> return "♣️"</span><br><span> }</span><br><span></span><br><span> var isRed: Bool {</span><br><span> case .Hearts, .Diamonds:</span><br><span> return true</span><br><span> case .Spades, .Clubs:</span><br><span> return false</span><br><span> }</span><br><span> }</span><br><span></span><br><span>* 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><span></span><br><span> enum Suit: Int {</span><br><span> var isRed: Bool { return false }</span><br><span></span><br><span> case Hearts {</span><br><span> let description: String { return "♥️" }</span><br><span> let isRed: Bool { return true }</span><br><span> }</span><br><span> case Spades {</span><br><span> let description: String { return "♠️" }</span><br><span> }</span><br><span> case Diamonds {</span><br><span> let description: String { return "♦️" }</span><br><span> let isRed: Bool { return true }</span><br><span> }</span><br><span> case Clubs {</span><br><span> let description: String { return "♣️" }</span><br><span> }</span><br><span></span><br><span> static var all = [ Hearts, Spades, Diamonds, Clubs ]</span><br><span> }</span><br></div></blockquote><blockquote type="cite"><div><span>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><span></span><br><span>-- </span><br><span>Brent Royal-Gordon</span><br><span>Architechies</span><br><span></span><br><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></body></html>