Nested types in Swift - what is the good practice?

末鹿安然 提交于 2020-01-10 04:56:06

问题


I have a swiftlint warning that bothers me.

warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)

However, the nesting of structs is an established programming technique, and quite a few people advocate it.

Edit: Indeed @vadian points out the Swift language guide's rule:To nest a type within another type, write its definition within the outer braces of the type it supports. Types can be nested to as many levels as are required.

I am aware it clashes with the use of generics, and that Xcode may become unbearably slow. It actually was (through measuring the slowest compilation spots) the reason why I started looking at this nesting rule.

What is the reason for the lint rule, and what is the good practice in that respect? Please point out the technical reasons, rather than purely opinion-based advice.

Microsoft actually has a page about nested types, and when they are appropriate. While it is not targeted at Swift, it does have some interesting trans-language thoughtbits.


回答1:


After much searching, all I've found is @jpsim's remark that "the idea behind the nesting rule is to avoid complex interfaces".

Therefore, apart from the compiler issues outlined in my question, which will eventually subside, there seems to not be any technical reason for this rule.



来源:https://stackoverflow.com/questions/42996275/nested-types-in-swift-what-is-the-good-practice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!