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

Haravikk swift-evolution at haravikk.me
Tue Mar 7 09:54:18 CST 2017


> On 7 Mar 2017, at 15:24, Saagar Jha via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I believe the indentation is more a signal for a new scope than curly braces. Swift doesn’t indent its switch cases since they’re a part of the same scope, while nested types and methods are a new scope.

I'm not sure that it's Swift-specific, this just seems to be what Xcode does even if you're working in another language; I use Xcode to do PHP and Javascript as well (it's not the best for these but I already have it so it's an obvious choice), and it does the same thing for switch statements.

It's a matter of personal preference; personally I do indent switch cases but only if all of them are simple (i.e- the case and its content can be put on a single line without being too noisy looking), but if one or more of the cases are multi-line then I use the default (cases not indented, but contents are).

I think the point is that it's (sort of) one scope, since you can use the fallthrough keyword to process multiple cases if you want to, the switch and cases just determines at what point you dive into that scope.

It makes sense when you think about how switch statements actually execute; in essence the contents of each case are usually structured as one big lump of code, with jumps to break out of it once the case is complete. The switch itself is some form of conditional matching to pick which case is triggered, resulting in a jump to the correct location. While this is similar to how if statements work behind the scenes, you can't fall-through (only one branch is ever executed, never both).


More information about the swift-evolution mailing list