<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">Le 3 mars 2017 à 23:21, Matthew Johnson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><br class="Apple-interchange-newline">On Mar 3, 2017, at 9:24 AM, Karim Nassar via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""><br class="">I’ve read through the last couple of Swift (sub)Module proposals put forward, and since my particular use-cases for a sub-module solution seemed to be under-served by them, I’ve decided to write up my thoughts on the matter to prompt discussion.<span class="Apple-converted-space">&nbsp;</span><br class=""><br class="">Perhaps my use-cases are outliers, and my approach will be deemed naive by the community… I’m happy to learn better ways of doing things in Swift, and welcome any thoughts, criticism, or illumination related to these ideas.<br class=""><br class="">I’m including the write-up below, but it’s also available as a gist: <a href="https://gist.github.com/anonymous/9806f4274f1e13860670d6e059be5dce" class="">https://gist.github.com/anonymous/9806f4274f1e13860670d6e059be5dce</a><br class=""><br class="">—<br class=""><br class=""># Sub-modules<br class=""><br class="">A sub-module solution in Swift should have the following properties:<br class=""><br class="">* Extremely light-weight<br class="">* Low API surface area<br class="">* Adopt progressive disclosure<br class="">* Integrate with Access Control features to enable a level of encapsulation &amp; hiding between the Module and File level<br class="">* Be permeable when desired<br class=""><br class="">## Discussion<br class=""><br class="">As we get deeper into building real applications &amp; frameworks with Swift, we begin to realize that having a way to express relationships between types is desireable. &nbsp;Currently, Swift only allows us to express these relationships at two levels, the Module and the File.<span class="Apple-converted-space">&nbsp;</span><br class=""><br class="">The Module boundary is acceptable for small, focused frameworks, while the File boundary is acceptable for small, focused Types, but both levels can be unweildy when dealing with certain cases where a cluster of internally related types needs to know about each other but may only want to publish a narrow set of APIs to the surrounding code, or in large complex applications which are necessarily structured as a single Module. In these cases, we wind up with large monolithic Modules or (even worse) large monolithic Files.<br class=""><br class="">I have seen this proliferation of Huge Sprawling Files (HSFs) in my own code, and seek a way to combat this rising tide.<br class=""><br class="">## Goals<span class="Apple-converted-space">&nbsp;</span><br class=""><br class="">It is a goal of this proposal to:<br class=""><br class="">* Suggest a mechanism for organizing code between the Module and File levels that is as lightweight and low-friction as possible<br class="">* Provide mechanisms for authors to create both "hard" and "soft" API boundaries between the Module and File levels of their code<br class=""><br class="">## Anti-Goals<br class=""><br class="">It is not a goal of this proposal to:<br class=""><br class="">* Move Swift away from filesystem-based organization<br class="">* Significantly alter the current Access Control philosophy of Swift<br class=""><br class="">## Proposal Notes<br class=""><br class="">Please take the following proposal wholely as a Straw-Man... I would be equally satisfied with any solution which meets the critera described at the top of this document.<br class=""><br class="">Unless specified otherwise, all spellings proposed below are to be considered straw-men, and merely illustrative of the concepts.<br class=""><br class="">## Proposed Solution<br class=""><br class="">Two things are clear to me after using Swift and following the Swift Evolution list since their respective publications:<br class=""><br class="">1. Swift has a preference for file-based organization<br class="">2. Vocal Swift Users dislike `fileprivate` and want to revert to Swift2-style `private`<br class=""><br class="">Because of #1, this proposal does not seek to change Swift's inherent file-system organization, and instead will expand on it.<br class=""><br class="">Since I personally fall into the camp described by #2, and most of the community response to this has been "Lets wait to deal with that until sub-modules", I'm making this proposal assuming that solving that quagmire is in-scope for this propsoal.<br class=""><br class="">### Changes to Access Control Modifiers<br class=""><br class="">As part of this proposal, I suggest the following changes to Swift 3's Access Control modifiers:<br class=""><br class="">* Revert `private` to Swift 2's meaning: "hidden outside the file"<br class="">* Remove `fileprivate` as redundant<br class=""><br class="">This is potentially a source-breaking change. However, it is interesting to note that this change is **not** required for the following proposal to function.<br class=""><br class="">Changes that *are* necessary are:<br class=""><br class="">* Change the spelling of `internal` to `module` (making `module` the new default)<br class="">* Introduce a new modifier `internal` to mean "Internal to the current sub-module and its child-sub-modules”<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Can you give concrete examples of use cases where a descendent submodule needs access to symbols declared by an ancestor? &nbsp;I gave some thought to this when drafting my proposal and came to the conclusion that this runs against the grain of layering and is likely to be a bad idea in practice. &nbsp;If there are use cases I didn’t consider I am very interested in learning about them.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><br class="">These changes are *not* source-breaking because the new `internal` modifier acts exactly as the old `internal` modifier unless it is used within a sub-module. The specific spelling of this new `internal` modifier is necessary to maintain backwards source compatibility.<br class=""><br class="">The new `module` modifier allows authors to make APIs permeable between sub-modules while still hidden outside the owning Module if desired.<br class=""><br class="">All other Access Control modifiers behave the same as they currently do irrespective of sub-module boundaries, so:<br class=""><br class="">* `public` =&gt; Visible outside the Module<br class="">* `open` =&gt; Sub-classable outside the Module<br class=""><br class="">### Making a Sub-module<br class=""><br class="">To create a sub-module within a Module (or sub-module) is simple: The author creates a directory, and places a "sub-module declaration file" within the directory:<br class=""><br class="">```<br class="">// &nbsp;__submodule.swift<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Why the double underscore prefix? &nbsp;To make it sort to the top in a file browser?</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Is this file allowed to have any Swift code? &nbsp;Or is it limited to submodule-related declarations only? &nbsp;If the latter, why not use an extension such as `.submodule` or `.swiftmodule` to differentiate it from ordinary Swift files and allow the submodule to be named by the name of this file?</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">// &nbsp;MyModule<br class=""><br class="">submodule SubA<br class=""><br class="">```<br class=""><br class="">Then any files within that directory are part of the sub-module:<br class=""><br class="">```<br class="">// &nbsp;Foo.swift<br class="">// &nbsp;MyModule.SubA<br class=""><br class="">struct Foo {<br class="">&nbsp;&nbsp;private var mine: Bool<br class="">&nbsp;&nbsp;internal var sub: Bool<br class="">&nbsp;&nbsp;module var mod: Bool<br class="">}<br class=""><br class="">public struct Bar {<br class="">&nbsp;&nbsp;module var mod: Bool<br class="">&nbsp;&nbsp;public var pub: Bool<br class="">}<br class=""><br class="">```<br class=""><br class="">This creates a sub-module called "SubA" within the module "MyModule". All files within the directory in which this file appears are understood to be contained by this sub-module.<br class=""><br class="">If in the future we choose to add additional complexity (versioning, #availability, etc) to the sub-module syntax, the sub-module declaration gives a natural home for this configuration.<br class=""><br class="">It's important to note some benefits of this approach:<br class=""><br class="">* Using the "special file" means that not all Directories are automatically submodules<br class="">* Any given source file may only be a member of 1 submodule at a time<br class="">* Use of filesystem structure to denote sub-modules plays nicely with source control<br class="">* The sub-module structure is instantly clear whether using an IDE (which can be taught to parse the `__submodule.swift` files to decorate the UI), or simple text-editor (assuming a convention of naming the Directory the same as the sub-module, which is a linter problem)<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">If we’re going to use the file system to organize submodules this seems like a reasonable approach. &nbsp;It allows larger submodules to have folder hierarchies within them and also creates a central location for submodule-related declarations.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">A primary flaw I see in this approach is that Xcode is the dominant IDE for Swift and the way Xcode handles files is not conducive to file-system organization. &nbsp;I really detest the way Xcode handles this and would vastly prefer that it simply reflected the physical file system hierarchy but I don’t think that will change any time soon. &nbsp;On the other hand maybe a file system based submodule system in Swift would motivate the Xcode team to better reflect the physical file system organization.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><br class=""></div><div>I quite love how Xcode allow me to group my source files as I want without having to move them around.</div><div>This is specially useful when working with languages that support relative #include "". Moving an header in an other group don’t break the code.</div><div><br class=""></div><div>Moreover, if you want to create a folder for each group in Xcode, you can perfectly do that too as Xcode allows you to define a different path for each group. So I don’t see how it would prevent using submodule.</div><div><br class=""></div><div><br class=""></div><div><br class=""></div><div><br class=""></div><br class=""></body></html>