<div dir="ltr">Making enums non-exhaustive solves a problem for authors of precompiled frameworks which apps can dynamically link against. These libraries need to preserve binary compatibility, in the sense that apps should continue to work with newer versions of the library.<div><br></div><div>Now, that creates a problem for app developers, wherein the compiler no longer informs them when their switch statements don’t cover all known enum cases. This can be solved by introducing “future” for switch statements, which acts like “default” but will warn if it is provably reachable.</div><div><br></div><div>Perhaps the most common scenario, however, is developers who write multi-module apps. Such apps could include modules with the same author, modules included as source, and even precompiled binary modules. As long as these are all bundled with the app, they cannot possibly change out from under it at runtime, so binary-compatibility is not a concern.</div><div><br></div><div>I think it is important that any solution to the problems of dynamically linked libraries should not adversely impact authors of multi-module apps.</div><div><br></div><div>It sounds like John McCall has an approach to handle this with version locking of dependencies.</div><div><br></div><div>Another possibility is to say that public enums are non-exhaustive by default if the module is built with resilience enabled, but if resilience is not enabled then enums are exhaustive.</div><div><br></div><div>Alternatively, we could introduce a concept of “the entire thing I am building, including all its statically-linked modules”, and let enums be exhaustive within that domain while still defaulting to non-exhaustive outside it.</div><div><br></div><div>I am glad to see vigorous debate and brainstorming on this thread, and I am confident that we will find a good solution.</div><div><br></div><div>Nevin</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 21, 2017 at 2:26 PM, John McCall via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div><div class="h5"><blockquote type="cite"><div>On Dec 21, 2017, at 2:03 PM, Jordan Rose via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_6451503592278197819Apple-interchange-newline"><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br class="m_6451503592278197819Apple-interchange-newline"><br><blockquote type="cite"><div>On Dec 20, 2017, at 12:35, Karl Wagner &lt;<a href="mailto:razielim@gmail.com" target="_blank">razielim@gmail.com</a>&gt; wrote:</div><br class="m_6451503592278197819Apple-interchange-newline"><div><div style="word-wrap:break-word"><br><div><br><blockquote type="cite"><div>On 20. Dec 2017, at 19:54, Jordan Rose &lt;<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>&gt; wrote:</div><br class="m_6451503592278197819Apple-interchange-newline"><div><div style="word-wrap:break-word"><br><div><br><blockquote type="cite"><div>On Dec 20, 2017, at 05:36, Karl Wagner via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_6451503592278197819Apple-interchange-newline"><div><div style="word-wrap:break-word"><br><div><br><blockquote type="cite"><div>On 19. Dec 2017, at 23:58, Ted Kremenek via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_6451503592278197819Apple-interchange-newline"><div><div style="word-wrap:break-word"><p style="margin-right:0px;margin-bottom:15px;margin-left:0px;font-family:Helvetica,arial,sans-serif;background-color:rgb(255,255,255);margin-top:0px!important">The review of &quot;SE 0192 - Non-Exhaustive Enums&quot; begins now and runs through <strong>January 3, 2018</strong>.</p><p style="margin:15px 0px;font-family:Helvetica,arial,sans-serif;background-color:rgb(255,255,255)">The proposal is available here:</p><blockquote style="margin:15px 0px;border-left-width:4px;border-left-style:solid;border-left-color:rgb(221,221,221);padding:0px 15px;color:rgb(119,119,119);font-family:Helvetica,arial,sans-serif;background-color:rgb(255,255,255)"><div style="margin:0px"><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0192-non-exhaustive-enums.md" target="_blank">https://github.com/apple/<wbr>swift-evolution/blob/master/<wbr>proposals/0192-non-exhaustive-<wbr>enums.md</a></div></blockquote></div></div></blockquote><div>+1, it needs to happen (and ASAP, since it _will_ introduce source-breaking changes one way or the other).</div><div><br></div><div>I think non-exhaustive is the correct default. However, does this not mean that, by default, enums will be boxed because the receiver doesn’t know their potential size?</div></div></div></div></blockquote><div><br></div><div>It&#39;s not always boxing, but yes, there will be more indirection if the<span class="m_6451503592278197819Apple-converted-space"> </span><i>compiler</i> can&#39;t see the contents of the enum. (More on that below.)</div><div><br></div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div><div>That would mean that the best transition path for multi-module Apps would be to make your enums @exhaustive, rather than adding “default” statements (which is unfortunate, because I imagine when this change hits, the way you’ll notice will be complaints about missing “default” statements).</div></div></div></div></blockquote><div><br></div><div>Yep, that&#39;s going to be the recommendation. The current minimal-for-review implementation does not do this but I&#39;d like to figure out how to improve that; at the very least it might be a sensible thing to do in the migrator.</div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div><div><br></div><div>I do have some thoughts about how we could ease the transition (for this and other resilience-related changes), but it’s best to leave that to a separate discussion.</div><div><br></div><div>The one thing I’m still not overly fond of is the name - I would like us to keep the set of resilience/optimisation related keywords to a minimum. “exhaustive” for enums feels an awful lot like “fixed_contents” for structs - couldn’t we come up with a single name which could be used for both? I don’t think anybody’s going to want to use “exhaustive” for structs.</div></div></div></div></blockquote><br></div><div>The core team was very focused on this too, but I contend that &quot;exhaustive&quot; is not about optimization and really isn&#39;t even about &quot;resilience&quot; (i.e. the ability to evolve a library&#39;s API while preserving binary compatibility). It&#39;s a semantic feature of an enum, much like &#39;open&#39; or &#39;final&#39; is for classes, and it affects what a client can or can&#39;t do with an enum. For libaries compiled from source, it won&#39;t affect performance at all—the<span class="m_6451503592278197819Apple-converted-space"> </span><i>compiler</i> still knows the full set of cases in the<span class="m_6451503592278197819Apple-converted-space"> </span><i>current</i> version of the library even if the programmer is forced to consider future versions.</div><div><br></div><div>I&#39;m working on the fixed-contents proposal now, though it won&#39;t be ready for a while, and the same thing applies there: for structs compiled from source, the compiler can still do all the same optimizations. It&#39;s only when the library has binary compatibility concerns that we need to use extra indirection, and then &quot;fixed-contents&quot; becomes important. (As currently designed, it doesn&#39;t affect what clients can do with the struct at all.) This means that I don&#39;t expect a &quot;normal&quot; package author to write &quot;fixed-contents&quot; at all (however it ends up being spelled), whereas &quot;exhaustive&quot; is a fairly normal thing to consider whenever you make an enum public.</div><div><br></div><div>I hope that convinces you that &quot;fixed-contents&quot; and &quot;exhaustive&quot; don&#39;t need to have the same name. I don&#39;t think anyone loves the<span class="m_6451503592278197819Apple-converted-space"> </span><i>particular</i> name &quot;exhaustive&quot;, but as you see in the &quot;Alternatives considered&quot; we didn&#39;t manage to come up with anything significantly better. If reviewers all prefer something else we&#39;d consider changing it.</div><div><br></div><div>Thanks for responding!</div><div>Jordan</div><br></div></div></blockquote><br></div><div>When you say “libraries compiled from source”, what do you mean?</div></div></div></blockquote><div><br></div><div>- Other targets in your project</div><div>- Source packages built through SwiftPM / CocoaPods / Carthage / other</div><div><br></div><div>And I was being imprecise with the terminology, but also</div><div><br></div><div>- Libraries built by someone else but designed to be embedded into an app, so that there&#39;s no chance of a different version showing up at run-time.</div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div><br></div><div>As for whether its a resilience feature: actually it is completely a resilience feature. The effects on switching are only side-effects; really what “exhaustive” or “nonexhaustive” are saying is literally that cases may be added later. Even if we added private cases, you wouldn’t need to mark those enums as specially exhaustive or not; that would be implied. It’s an accommodation for things which don’t exist yet, so really, it is all about resilience IMO.</div></div></div></blockquote><div><br></div><div>&quot;Resilience&quot;, as an admittedly fuzzily-defined term in the Swift project, <a href="https://github.com/apple/swift/blob/master/docs/Lexicon.rst" target="_blank">specifically refers to what changes can be made without breaking binary compatibility</a>. It does not refer to<span class="m_6451503592278197819Apple-converted-space"> </span><i>every</i> change you can make to a library. (For comparison, adding a field to a struct is not source-breaking in Swift. We would like to make it not ABI-breaking either; that proposal&#39;s coming soon.)</div><div><br></div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div><br></div><div>Anyway, as I see it, library authors in general ought to be happy about this:</div><div>+ Their libraries become safer by default, so they can make changes in the future without having to worry about breakage</div><div>+ It doesn’t affect your code inside of a module, so it only affects types they already explicitly marked “public”</div></div></div></blockquote><div><br></div><div>That&#39;s the intent.</div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div><br></div><div>The only people who lose are multi-module App developers, because they are “library authors” who don’t need to care about evolution, and now need to add attributes to things they wouldn’t have to before, or suffer language and performance penalties. Their libraries become less reusable and not resilient-by-default.</div><div><br></div><div>For example, I have an App for which I wrote a cross-platform model framework in Swift. When I compile it as a framework inside my App, it is bundled there forever. However, I use the same code to build libraries for Linux, which I would like to ship in binary form to 3rd-parties. Am I supposed to litter my code with annotations to mark those types as final, just to make the App fast and convenient to code? What happens when I need to fix a bug and distribute an updated copy, this means the 3rd-parties need to recompile (which they won’t do…).</div><div><br></div><div>Typically, for such a problem, I would recommend using a static library instead. But we don’t have those, and anyway they’re not always the best thing these days. So that’s why I started a new thread about creating a “@static” import, so App developers can go back to all the conveniences they had before.</div></div></div></blockquote><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">There won&#39;t be a perf penalty, but yes, I do expect multi-module apps to use &#39;exhaustive&#39; on most of their enums, because they don&#39;t need the futureproofing. Maybe this should have been mentioned more explicitly in the proposal.</div></div></blockquote><div><br></div></div></div>As a perhaps more long-term design note, I think modules ought to have the ability to version-lock themselves to one or more of their dependencies.  They would still be required to obey access control as if they were outside those dependencies, but we would suppress some of the <i>semantic</i> consequences of being outside the module, such as the need to assume non-exhaustiveness by default.</div><div><br></div><div>That is, there would be two independent axes of library dependency: source vs. binary and version-compatible vs. version-locked:</div><div>  - a source dependency allows the compiler to take advantage of the implementation of public entities when generating code</div><div>  - a version-locked dependency allows the compiler to take advantage of the implementation of public entities when enforcing semantics</div><div><br></div><div>Apps would generally elect to primarily use version-locked source dependencies because they&#39;re just pulling down source libraries (e.g. from github) and are comfortable with updating their code if the library changes.</div><div><br></div><div>Source libraries on github would generally want to use version-compatible source dependencies because version-locking would put their clients in &quot;library hell&quot; if the locking didn&#39;t all agree.</div><div><br></div><div>Binary dependencies could reasonably use either.</div><div><br></div><div>John.</div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>