<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">+1 for adding Java behaviour, it is something I have missed.<br class=""><br class="">The example would be:<br class=""><br class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">enum Suit: Int,&nbsp;CustomStringConvertible&nbsp;{</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;case Hearts {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var description: String { return “♥️" }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;case Spades {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var description: String { return “♠️" }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;case Diamonds {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var description: String { return “♦️" }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;case Clubs {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var description: String { return “♣️" }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span></div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;// ...</div>}</blockquote><div class=""><br class="">The compiler could automatically generate the switch statement if that was better than virtual dispatch.<br class=""><br class=""></div><br class=""><blockquote type="cite" class="">On 25 Mar 2016, at 2:42 AM, David Waite via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">In Java, the enum type behaves as an abstract class, sealed against the case members (which are singleton instance subclasses.)<br class=""><br class="">While Swift enums aren’t discrete types or singletons, it sounds like what you would like is the ability to have an enum behave as so - to be able to override base (or protocol extension)&nbsp;behavior with a particular enum case, and have that translated most likely into a switch statement (most likely - I suppose if you are using witness tables it could optimize the switch away)<br class=""><br class="">Actually with a protocol default behavior being overridden with a single enum case, this would give you functionality not possible today (referencing that protocol extension method)<br class=""><br class="">In Java, I exploit the enum behavior to implement the State design pattern quite a bit, but am limited as Java enums are singletons and thus should be isolated from state. Swift enums are&nbsp;even more powerful here, but doing this in switch statements is a pain for maintainability.<br class=""><br class="">I like the idea<br class=""><br class="">-DW<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Mar 23, 2016, at 4:13 AM, Brent Royal-Gordon via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br 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,&nbsp;code so old we were still using the `T[]` shorthand syntax:<br class=""><br class="">&nbsp; enum Suit: Int {<br class="">&nbsp; &nbsp; &nbsp; case Hearts, Spades, Diamonds, Clubs<br class=""><br class="">&nbsp; &nbsp; &nbsp; static var all: Suit[] { return [ Hearts, Spades, Diamonds, Clubs ] }<br class=""><br class="">&nbsp; &nbsp; &nbsp; var description: String {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch(self) {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case .Hearts:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "♥️"<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case .Spades:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "♠️"<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case .Diamonds:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "♦️"<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case .Clubs:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "♣️"<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; &nbsp; }<br class=""><br class="">&nbsp; &nbsp; &nbsp; var isRed: Bool {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch(self) {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case .Hearts, .Diamonds:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case .Spades, .Clubs:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; &nbsp; }<br class="">&nbsp; }<br class=""><br class="">It would be nice if we could somehow eliminate that. I have two suggestions:<br class=""><br 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).<br class=""><br class="">&nbsp; enum Suit: Int {<br class="">&nbsp; &nbsp; &nbsp; case Hearts, Spades, Diamonds, Clubs<br class=""><br class="">&nbsp; &nbsp; &nbsp; static var all = [ Hearts, Spades, Diamonds, Clubs ]<br class=""><br class="">&nbsp; &nbsp; &nbsp; var description: String {<br class="">&nbsp; &nbsp; &nbsp; case .Hearts:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "♥️"<br class="">&nbsp; &nbsp; &nbsp; case .Spades:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "♠️"<br class="">&nbsp; &nbsp; &nbsp; case .Diamonds:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "♦️"<br class="">&nbsp; &nbsp; &nbsp; case .Clubs:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "♣️"<br class="">&nbsp; &nbsp; &nbsp; }<br class=""><br class="">&nbsp; &nbsp; &nbsp; var isRed: Bool {<br class="">&nbsp; &nbsp; &nbsp; case .Hearts, .Diamonds:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true<br class="">&nbsp; &nbsp; &nbsp; case .Spades, .Clubs:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false<br class="">&nbsp; &nbsp; &nbsp; }<br class="">&nbsp; }<br class=""><br 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.<br class=""><br class="">&nbsp; enum Suit: Int {<br class="">&nbsp; &nbsp; &nbsp; var isRed: Bool { return false }<br class=""><br class="">&nbsp; &nbsp; &nbsp; case Hearts {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let description: String { return "♥️" }<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let isRed: Bool { return true }<br class="">&nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; &nbsp; case Spades {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let description: String { return &nbsp;"♠️" }<br class="">&nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; &nbsp; case Diamonds {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let description: String { return &nbsp;"♦️" }<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let isRed: Bool { return true }<br class="">&nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; &nbsp; case Clubs {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let description: String { return &nbsp;"♣️" }<br class="">&nbsp; &nbsp; &nbsp; }<br class=""><br class="">&nbsp; &nbsp; &nbsp; static var all = [ Hearts, Spades, Diamonds, Clubs ]<br class="">&nbsp; }<br class=""><br 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.<br class=""><br class="">--&nbsp;<br class="">Brent Royal-Gordon<br class="">Architechies<br class=""><br class="">_______________________________________________<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=""></blockquote><br class="">_______________________________________________<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=""></blockquote><br class=""></body></html>