[swift-evolution] class indent in swift, history?
Will Stanton
willstanton1 at yahoo.com
Wed Mar 8 23:24:48 CST 2017
Hello Chris, and all,
Thanks for the replies and explanation!
There have been moments when I felt like it would be nice if most functions could start at the 1-indent level. But I think I agree: true/consistent nesting is good motivation for the additional indent level. Still, in those times, I wondered why Swift wasn’t more Obj-C-like. So, now I know, and thanks again!
Regards,
Will Stanton
> On Mar 8, 2017, at 1:29 AM, Chris Lattner <clattner at nondot.org> wrote:
>
> Hi Will,
>
> Good question: the general rationale is that true nesting implies indentation in Swift. Cases are not indented under “switch” because they are part of the switch statement, and, similarly, didSet is part of the property it is defined on. In contrast, nested types really are nested, they aren’t part of the enclosing declaration.
>
> -Chris
>
>
>> On Mar 6, 2017, at 10:42 PM, Will Stanton <willstanton1 at yahoo.com> wrote:
>>
>> Hello Chris and perhaps core team members,
>>
>> The comments on tab-levels for `switch` made me want to ask about the considerations that went into class and protocol-level indentation.
>>
>> Sorry if my wording isn’t precise, but in Objective-C, functions can (should) be at the same 0-indent level as the class:
>> @implementation Foo
>> // No indent!
>> - (void)doSomething {
>> }
>> @end
>>
>> However, in Swift, method and nested types are indented by default:
>> class Foo : Bar {
>> // Things indented!
>> enum Types {
>> }
>> func doSomething() {
>> }
>> }
>>
>>
>> Was the change mostly driven by the desire for protocol+class+struct+enum consistency in ‘increasing’ the scope+indent level? Were there other considerations?
>> Why didn’t the language evolve into something like this to reduce the use of horizontal whitespace, allowing class functions/types at the root/top level? Like:
>> @class Foo : Bar
>> enum Types {
>> }
>> func doSomething() {
>> }
>> @end
>>
>>
>> Regards,
>> Will Stanton
>>
>>> On Mar 7, 2017, at 12:52 AM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
>>>
>>> I can understand how you might find this unnerving, but it is important to understand that Swift and Objective-C/C have different semantics when it comes to case labels: in Swift, a case label *is* a scope, and *is* part of the switch statement. In Objective-C, a case label is just a label, like any other goto label: it is not a scope and is not necessarily a direct child of the switch statement.
>>>
>>> C and Objective-C’s behavior is what leads to obscure but important things like Duff’s device (https://en.wikipedia.org/wiki/Duff's_device).
>>>
>>> In contrast, Swift fixes the scoping, fallthrough, and other related problems all in one fell swoop, and ensures that cases are directly nested under the switch (unlike in C, where they can be nested under other statements within the switch). Because the case/default labels are *part of* the switch in Swift, it makes sense for them to be indented at the same level.
>>>
>>> While I can respect that you aesthetically have a preference for the Objective-C way of doing things, the rationale for this behavior change wasn’t arbitrary and wasn’t due to "LLVM style". It is an important reflection of the core semantics of the language model.
>>>
>>> Finally, conservation of horizontal whitespace is important for comprehension of code, particularly w.r.t. readability and maintenance. This is why statements like guard exist: to reduce nesting and indentation, directing the attention of someone reading and maintaining code to the important parts.
>>>
>>
>
More information about the swift-evolution
mailing list