[swift-evolution] class indent in swift, history?

Haravikk swift-evolution at haravikk.me
Wed Mar 8 18:02:52 CST 2017


> On 8 Mar 2017, at 17:23, Rob Mayoff via swift-evolution <swift-evolution at swift.org> wrote:
> 
> On Wed, Mar 8, 2017 at 5:09 AM, Haravikk via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> 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.
> 
> A switch statement has a separate scope for every case, including default. Example:
> 
> switch Int() {
> case 0:
>     let m = "zero"
>     fallthrough
> default:
>     Swift.print(m)
> }
> 
> Result:
> 
> Playground execution failed: error: MyPlayground.playground:2:17: error: use of unresolved identifier 'm'
>     Swift.print(m)

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:

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.

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:

	if (foo == 1) jump @case1
	else if (foo == 2) jump @case2
	else jump @default

	@case1
		print("Case 1")
		jump @end
	@case2
		print("Case 2")
		// no jump because of fallthrough
	@default
		print("Default")
		jump @end
	@end

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170309/837a1622/attachment.html>


More information about the swift-evolution mailing list