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

Haravikk swift-evolution at haravikk.me
Wed Mar 8 05:09:15 CST 2017


> On 8 Mar 2017, at 08:35, Adrian Zubarev via swift-evolution <swift-evolution at swift.org> wrote:
> 
> The latter slightly confused me for a second and I had to double check. You’re saying that cases are not indented “because” they are part of the switch statement, but observers like willSet and didSet are indented even if they are part of the property they are defined on. What’s the point I’m missing in this argument? (I’m precisely talking about the comparison between cases and observers, not about the indent elsewhere.)
> 

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.

To try and illustrate, consider the following switch statement:

	switch(foo) {
	case 1:
		print("Case 1")
	case 2:
		print("Case 2")
		fallthrough
	default:
		print("Default")
	}

If foo is equal to 2, the output will be both "Case 2" and "Default". Another way to think of this is in terms of how a switch statement actually executes, the above would become something resembling the following (using rough pseudo-code):

	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

I don't know if that helps clear it up, but while syntactically a switch statement's cases are grouped with the code that they trigger, this is just because it's easier to work with; in reality the entire content of the switch is one contiguous run of code that you jump into (and possibly out of) as determined by some form of test at the start. The ability for a case to fallthrough (no jump to the end) means that it's possible to execute multiple cases, unlike an if-statement where only one branch is ever executed.

The lack of indentation is a good reminder that a case should be thought of more as a label, and that its condition is actually checked at the switch(foo) part of the construct.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170308/605c4b58/attachment.html>


More information about the swift-evolution mailing list