<html><body><div>Am 11. Mai 2016 um 05:31 schrieb Chris Lattner via swift-evolution <swift-evolution@swift.org>:<br><br><div><blockquote type="cite"><div class="msg-quote" style="word-wrap: break-word;" data-mce-style="word-wrap: break-word;"><br class=""><div><blockquote type="cite" class=""><div class="">On May 10, 2016, at 4:33 AM, Sam Dods via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="word-wrap: break-word;" class="" data-mce-style="word-wrap: break-word;"><div class="">I propose that <span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo"><b class="">(case .Foo = bar)</b></span> should be treated as an expression with a Boolean value, so the result can be set to a variable or returned from a method.</div></div></div></blockquote><div><br class=""></div><div>I agree that this is an important use case that Swift doesn’t serve well right now, but I don’t think this is the right way to go. </div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word;" class="" data-mce-style="word-wrap: break-word;"><div class="">Considering the following enumeration:</div><div class=""><br class=""></div><div class=""><span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo"><b class="">enum Bar {</b></span></div><div class=""><span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo"><b class=""> case foo(name: String)</b></span></div><div class=""><span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo"><b class=""> case notFoo</b></span></div><div class=""><span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo"><b class=""> case unknownFoo</b></span></div><div class=""><span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo"><b class="">}</b></span></div></div></div></blockquote><div><br class=""></div><div>One of the things we’ve discussed in the past is that we could have enums automatically “synthesize” instance members for projecting cases as optional values or bools. For example, the above enum could be compiled into the equivalent of:</div><div><br class=""></div><div>extension Bar {</div><div> func getAsFoo() -> String? { … }</div><div> var isNotFoo : Bool { … }</div><div> var isUnknownFoo : Bool { … }</div><div>}</div><div><br class=""></div><div>Then you could just use:</div><div><br class=""></div><div> if someBar.isUnknownFoo { … }</div><div> if someBar.isFoo != nil { … }</div><div> if let name = someBar. getAsFoo() {... }</div><div> someBar. getAsFoo()?.doThing() </div></div></div></blockquote></div><div><span><br data-mce-bogus="1"></span></div><div><span>I would strongly prefer pattern matching over magic methods, because of the following reasons:<br></span></div><div><span><br data-mce-bogus="1"></span></div><div><span>(a) IMHO the latter are confusing because it is not clear and visible which methods have been created <br></span></div><div><span>(b) patterns allow IDE support for browsing usages of the various enums whereas browsing for a bunch of invisible automagically created methods is much more difficult and confusing</span></div><div><span>(c) I can't see a way of generating nice methods for accessing associated values (in various combinations) (see getAsFoo() example above and imagine a second or third parameter to Bar.foo)</span></div><div><span><br data-mce-bogus="1"></span></div><div><span>-Thorsten</span></div><div><span><br data-mce-bogus="1"></span></div><div><span><br></span><blockquote type="cite"><div class="msg-quote" style="word-wrap: break-word;" data-mce-style="word-wrap: break-word;"><div><div></div><div><br class=""></div><div>etc. There is a question of naming, and getting the details right, of course.</div><div><br class=""></div><div>-Chris</div></div><div class="_stretch"><span class="body-text-content">_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></div></blockquote></div></div></body></html>