<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">This pattern matching syntax is very nice, for sure.</div><div class="">I think the proposed evolution would be a nice complement to it in two ways:</div><div class="">- being able to handle the case of optional using case? would avoid having to declare a case (.null in the exemple) that is purely technical, and does not match what the enum describes ;</div><div class="">- being able to assign a constant or variable outside of a flow control structure like “if" or “guard" can be useful (if you need to use the variable in multiple distinct places for exemple, be it in the same method, in different methods, or even in an instance variable) ;</div><div class=""><br class=""></div><div class="">But your point emphasise the second drawback I listed : in all those existing syntaxes, the parenthesis after the enum case contains the variable in which the payload will be stored.</div><div class="">The syntax I am suggesting does not follow the same logic, which is not nice.</div><div class=""><br class=""></div><div class="">That syntax would be closer:</div><div class=""><br class=""></div><div class=""><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 11px;" class="">let</span> <span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 11px;" class="">case?</span> <span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">.</span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);">enumCase</span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">(payloadItem1, payloadItem2) = </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">input</span></div><div class=""><br class=""></div><div class="">It is pretty similar to the syntax used for pattern matching in guard, for and if.</div><div class="">The let and case keywords are swapped, the case gains a ‘?’ to reflect the fact that it will produce an optional, but the rest is the usual pattern matching syntax.</div><div class=""><br class=""></div><div class="">This solution would be even closer:</div><div class=""><br class=""></div><div class=""><div class=""><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 11px;" class="">case?</span> <span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 11px;" class="">let</span> <span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">.</span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);">enumCase</span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">(payloadItem1, payloadItem2) = </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">input</span></div></div><div class=""><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);"><br class=""></span></div><div class="">It mostly boils down to removing the guard keyword and its associated else.</div><div class="">But I feel very weird not having the let or var at the beginning of the line. Is that only me being too used to that, or does that matter for you too ?</div><div class=""><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);"><br class=""></span></div><div class="">Jerome</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 27 Sep 2016, at 01:04, Robert Widmann <<a href="mailto:devteam.codafi@gmail.com" class="">devteam.codafi@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Being a power user of this feature, I don’t think the existing syntax is cumbersome enough to warrant this kind of shortcut. I really like being able to</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">guard</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">dict</span><span style="font-variant-ligatures: no-common-ligatures" class="">(book) = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">data</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">else</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="white-space:pre" class="Apple-tab-span">        </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">// bail out</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">guard</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">dict</span><span style="font-variant-ligatures: no-common-ligatures" class="">(author) = book[</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"author"</span><span style="font-variant-ligatures: no-common-ligatures" class="">] ?? .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">null</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">else</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="white-space:pre" class="Apple-tab-span">        </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">// bail out</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">guard</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">integer</span><span style="font-variant-ligatures: no-common-ligatures" class="">(age) = author[</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"age"</span><span style="font-variant-ligatures: no-common-ligatures" class="">] ?? .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">null</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">else</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="white-space:pre" class="Apple-tab-span">        </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">// bail out</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// now we have the age</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><span class="">or more succinctly<br class=""></span><span class=""><br class=""></span><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">guard</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">dict</span><span style="font-variant-ligatures: no-common-ligatures" class="">(book) = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">data</span><span style="font-variant-ligatures: no-common-ligatures" class="">,</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> <span style="color: rgb(186, 45, 162);" class="">case</span> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">dict</span><span style="font-variant-ligatures: no-common-ligatures" class="">(author) = book[</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"author"</span><span style="font-variant-ligatures: no-common-ligatures" class="">] ?? .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">null</span><span style="font-variant-ligatures: no-common-ligatures" class="">,</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> <span style="color: rgb(186, 45, 162);" class="">case</span> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);" class="">integer</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(age) = author[</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">"age"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">] ?? .</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);" class="">null</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">else</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="white-space:pre" class="Apple-tab-span">        </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">// bail out</span></div><div style="margin: 0px; line-height: normal; color: rgb(62, 30, 129);" class=""><span style="" class="">}</span></div></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// now we have the age</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><span class="">Because it forces me to think about the bailout case(s) and is really not that much longer than your proposed syntax</span><div class=""><span class=""><br class=""></span></div><div class=""><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 11px;" class="">guard </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><span style="color: rgb(186, 45, 162);" class="">let</span> book = </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><span style="color: rgb(186, 45, 162);" class="">case</span>? .dict(inputData), </span></div><div class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class=""> </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><span style="color: rgb(186, 45, 162);" class="">let</span> author = </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><span style="color: rgb(186, 45, 162);" class="">case</span>? .dict(book?[</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"author”</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">]), </span></div><div class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class=""> </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><span style="color: rgb(186, 45, 162);" class="">let</span> age = </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><span style="color: rgb(186, 45, 162);" class="">case</span>? .integer(author?[</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"age”</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">])</span></div><div class=""><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">else</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="white-space: pre;" class="Apple-tab-span">        </span></span><span style="font-variant-ligatures: no-common-ligatures;" class="">// bail out</span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(62, 30, 129);" class=""><span style="" class="">}</span></div></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(62, 30, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">// now we have the age</span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">~Robert Widmann</span></div></span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(62, 30, 129);" class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div class="">On Sep 26, 2016, at 11:51 AM, Jérôme Duquennoy via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class=""><div style="font-family: HelveticaNeue; font-size: 17px;" class=""><b class="">Summary</b></div><div style="font-family: HelveticaNeue; font-size: 12px;" class="">The aim of this proposal is to offer a new syntax to ease some uses of enums with payload.</div><div style="font-family: HelveticaNeue; font-size: 12px;" class=""><br class=""></div><div style="font-family: HelveticaNeue; font-size: 17px;" class=""><b class="">Situation to improve:</b></div><div style="font-family: HelveticaNeue; font-size: 12px;" class="">Enums makes it possible to have explicate typing where it was not possible before. A classic example of that is filling a dictionary with data coming from a file or a stream (json, plist, …) : the types of possible values is finite : arrays, dicts, int, double, bool or string for json for exemple.</div><div style="font-family: HelveticaNeue; font-size: 12px;" class="">An enum can represent this finite range of possible types, its make the code and the API more self-documented.</div><div style="font-family: HelveticaNeue; font-size: 12px;" class="">Then, you have two possibilities to deal with this enum:</div><div style="font-family: HelveticaNeue; font-size: 12px;" class="">- using switch statements</div><div style="font-family: HelveticaNeue; font-size: 12px;" class="">- using the if case syntax introduced by swift 2</div><div style="font-family: HelveticaNeue; font-size: 12px;" class=""><br class=""></div><div style="font-family: HelveticaNeue; font-size: 12px;" class="">The drawback is that those two solutions can lead to writing code with high visual complexity, even though the logic behind is pretty simple.</div><div style="font-family: HelveticaNeue; font-size: 12px;" class=""><br class=""></div><div style="font-family: HelveticaNeue; font-size: 12px;" class="">Consider this example of a data dictionary, that a web service could have returned:</div><div style="font-family: HelveticaNeue; font-size: 12px;" class="">- book</div><div style="font-family: HelveticaNeue; font-size: 12px;" class=""> - title: </div><div style="font-family: HelveticaNeue; font-size: 12px;" class=""> - author:</div><div style="font-family: HelveticaNeue; font-size: 12px;" class=""> - name: Apple</div><div style="font-family: HelveticaNeue; font-size: 12px;" class=""> - age: 40</div><div style="font-family: HelveticaNeue; font-size: 12px;" class=""><br class=""></div><div style="font-family: HelveticaNeue; font-size: 12px;" class="">We can decode this in a variable of type [String:Value], where Value is:</div><div class=""><br class=""></div></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">enum</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Value {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> integer(value: </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> string(value: </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> dict(value: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Value</span><span style="font-variant-ligatures: no-common-ligatures" class="">])</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> null</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class="">Here is a snippet of code to access the age of the author:</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .dict(let book) = data {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .dict(let author) = book[</span><span style="font-variant-ligatures: no-common-ligatures; color: #b4261a" class="">"author"</span><span style="font-variant-ligatures: no-common-ligatures" class="">] ?? .null {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .integer(let age) = author[</span><span style="font-variant-ligatures: no-common-ligatures; color: #b4261a" class="">"age"</span><span style="font-variant-ligatures: no-common-ligatures" class="">] ?? .null {</span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// now we have the age</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">The multiple indentation levels can rapidly make this code unattractive to read, and we need to add a null case to the enum to deal with optional values.</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="font-size: 16px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">Proposed solution:</b></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">I suggest to add a new syntax, using the case keyword to ease access to the payload of such enums :</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> payloadContent = </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class="">? .enumCase(variable)</span></b></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">The payloadContent variable will be an optional, that can be either nil, or contain the payload of enumCase.</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">If the payload contains multiple variables, payloadContent will be a tupple.</span></div><div style="margin: 0px; line-height: normal;" class="">This syntax can accommodate an optional variable as an input. If the value of variable is nil, then payloadContent will be nil.</div><div style="margin: 0px; line-height: normal;" class="">Only enum cases with a payload can be used with this syntax (it would make no sens for cases without a payload).</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">With that syntax, the null case of the enum can be removed, and the code to access the age becomes:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> book = </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class="">? .dict(inputData)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> author = </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class="">? .dict(book?[</span><span style="font-variant-ligatures: no-common-ligatures; color: #b4261a" class="">"author"</span><span style="font-variant-ligatures: no-common-ligatures" class="">])</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> age = </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class="">? .integer(author?[</span><span style="font-variant-ligatures: no-common-ligatures; color: #b4261a" class="">"age"</span><span style="font-variant-ligatures: no-common-ligatures" class="">])</span></div></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 14px;" class=""><b class="">Advantages:</b></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">- It leverages the well established notion of optional, and similar logic already exists in the language (for the as? operator notably)</span>.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">- It does not add a new keyword</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">- It promotes the use of enum to enforce explicit typing, which leads to more self-documenting code</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">- It reduces the complexity of the code in situations such as the one of the exemple</span></div><div class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div></div><div style="margin: 0px; line-height: normal; font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 14px;" class=""><b class="">Drawbacks:</b></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">- It adds a third use of the case keyword. </span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">- In the proposed syntax, the variable between parenthesis is not the payload, but the variable to decode. This might be disturbing, as it differs from the other syntax of enum values.</span></div><div style="margin: 0px; line-height: normal;" class="">- If the payload is an optional, it is not possible to differentiate a non-matching case and a matching case a nil payload.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 14px;" class=""><b class="">Alternatives:</b></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">- Another syntax without parenthesis could be used to avoid the second drawback:</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> payload = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? .enumCase variable</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><b style="font-size: 14px;" class="">Impact on existing code:</b></span></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">None, this is adding a new syntax</span></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">This proposal would have no impact on the ABI, so it probably does not fit the stage 1 of swift 4’s roadmap. But I would be glad to have your feedback, so that I can have a proposal ready once we enter stage 2.</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">So what your thoughts on that proposal ?</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">Thanks</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class="">Jerome</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></span></div></span></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></body></html>