<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 <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> 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, 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() // -> Feedback</font></div><div class=""><font face="Menlo" class="">if let msg = foo.msg { // since not all cases can hold a ‘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> case c1(T1)</div><div> case c2(T2)</div><div> ...</div><div> case cN(TN)</div><div><br></div><div> var description: String {</div><div> switch self {</div><div> case .c1(let val): return "\(val)"</div><div><div><span style="background-color: rgba(255, 255, 255, 0);"> case .c2(let val): return "\(val)"</span></div></div><div> ...</div><div><div><span style="background-color: rgba(255, 255, 255, 0);"> case .cN(let val): return "\(val)"</span></div></div><div> }</div><div> }</div><div>}</div><div><br></div><div>Being able to simplify that to:</div><div>var description: String {</div><div> let nilDesc = "some appropriate description"</div><div> return "\(self.0 ?? nilDesc)"</div><div>}</div><div><br></div><div>Would be great.</div><div><br></div><div>- Dave Sweeris </div></body></html>