<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 8 Mar 2017, at 17:23, Rob Mayoff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">On Wed, Mar 8, 2017 at 5:09 AM, Haravikk via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="">willSet and didSet have their own distinct scope; they execute independently, however a switch statement is effectively a single scope because of the ability to use fallthrough to visit later cases.</div><div class=""></div></blockquote></div><br class="">A switch statement has a separate scope for every case, including default. Example:</div><div class="gmail_extra"><br class=""></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class="gmail_extra"><div class="gmail_extra">switch Int() {</div></div><div class="gmail_extra"><div class="gmail_extra">case 0:</div></div><div class="gmail_extra"><div class="gmail_extra">&nbsp; &nbsp; let m = "zero"</div></div><div class="gmail_extra"><div class="gmail_extra">&nbsp; &nbsp; fallthrough</div></div><div class="gmail_extra"><div class="gmail_extra">default:</div></div><div class="gmail_extra"><div class="gmail_extra">&nbsp; &nbsp; Swift.print(m)</div></div><div class="gmail_extra"><div class="gmail_extra">}</div></div></blockquote><div class="gmail_extra"><div class="gmail_extra"><br class=""></div><div class="gmail_extra">Result:</div><div class="gmail_extra"><br class=""></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra">Playground execution failed: error: MyPlayground.playground:2:17: error: use of unresolved identifier 'm'</div></div></div><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra">&nbsp; &nbsp; Swift.print(m)</div></div></div></blockquote></div></div></blockquote></div><br class=""><div class="">I think I'm using the term scope in a confusing way, but I think I thought of a better way to explain it anyway:</div><div class=""><br class=""></div><div class="">When it comes to indenting the switch, the switch itself, and its cases, are actually all part of the same "level", because they're actually a leading conditional plus a bunch of labels to jump to, no new scope has occurred yet. It's only once you've jumped to a label that any kind of narrowing can actually occur.</div><div class=""><br class=""></div><div class="">To go back to my example of how a switch statement actually works, here's what it looks like again with jumps and labels, this time with case contents indented to show variable scope:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if (foo == 1) jump @case1</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>else&nbsp;if (foo == 2) jump @case2</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>else&nbsp;jump @default</font></div><div class=""><font face="Monaco" class=""><br class=""></font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>@case1</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>print("Case 1")</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>jump @end</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>@case2</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>print("Case 2")</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>// no jump because of fallthrough</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>@default</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>print("Default")</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>jump @end</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>@end</font></div></div><div class=""><br class=""></div><div class="">Perhaps the term block would have been better; the switch contents are a contiguous block of code that you jump into, the variable scope occurs because you can't guarantee that a previous case was visited and fell through. Thus the case conditions/labels themselves are not of a more limited scope because they're testing for and setting up a jump to the correct location. Just like how an if statement's condition isn't indented, because they're essentially the same thing.</div></body></html>