<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br></div><div>On Jan 8, 2017, at 06:53, Karim Nassar via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">One area of enums that I’d love to see some sugar wrapped around (and perhaps this has already been discussed previously?) is extracting associated values.</div><div class=""><br class=""></div><div class="">There are many times where, given an enum like:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">enum Feedback {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case ok</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case info(String)</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case warning(String,&nbsp;Location)</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case error(String, Location)</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">I’d love it if we could tag the associated values with some semantic accessor, perhaps borrowed from tuples:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class="">enum Feedback {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case ok</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case info(msg: String)</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case warning(msg: String, loc: Location)</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case error(msg: String, loc: Location)</font></div><div class=""><font face="Menlo" class="">}</font></div></div><div class=""><br class=""></div><div class="">then:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">let foo = self.getSomeFeedback() // -&gt; Feedback</font></div><div class=""><font face="Menlo" class="">if let msg = foo.msg { // since not all cases can hold a&nbsp;‘msg’ .msg is an Optional</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print(foo)</font></div><div class=""><font face="Menlo" class="">}</font></div></div></div></blockquote><br><div>Can't remember if it's come up before, but +1. I can't count how many times I've written something like:</div><div>enum Foo : CustomStringConvertible {</div><div>&nbsp; &nbsp; case c1(T1)</div><div>&nbsp; &nbsp; case c2(T2)</div><div>&nbsp; &nbsp; ...</div><div>&nbsp; &nbsp; case cN(TN)</div><div><br></div><div>&nbsp; &nbsp; var description: String {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; switch self {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case .c1(let val): return "\(val)"</div><div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case .c2(let val): return "\(val)"</span></div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...</div><div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case .cN(let val): return "\(val)"</span></div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>}</div><div><br></div><div>Being able to simplify that to:</div><div>var description: String {</div><div>&nbsp; &nbsp; let nilDesc = "some appropriate description"</div><div>&nbsp; &nbsp; return "\(self.0 ?? nilDesc)"</div><div>}</div><div><br></div><div>Would be great.</div><div><br></div><div>- Dave Sweeris&nbsp;</div></body></html>