{"id":44177,"date":"2024-07-22T14:15:47","date_gmt":"2024-07-22T18:15:47","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=44177"},"modified":"2024-09-23T14:05:15","modified_gmt":"2024-09-23T18:05:15","slug":"swift-6-announced","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/07\/22\/swift-6-announced\/","title":{"rendered":"Swift 6 Announced"},"content":{"rendered":"<p><a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2024\/10136\/\">What&rsquo;s new in Swift<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2024\/10136\/\"><p>We&rsquo;ll briefly go through a history of Swift over the past decade, and show you how the community has grown through workgroups, expanded the package ecosystem, and increased platform support. We&rsquo;ll introduce you to a new language mode that achieves data-race safety by default, and a language subset that lets you run Swift on highly constrained systems. We&rsquo;ll also explore some language updates including noncopyable types, typed throws, and improved C++ interoperability.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2024\/10169\/\">Migrate your app to Swift 6<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2024\/10169\/\">\n<p>Experience Swift 6 migration in action as we update an existing sample app. Learn how to migrate incrementally, module by module, and how the compiler helps you identify code that's at risk of data races. Discover different techniques for ensuring clear isolation boundaries and eliminating concurrent access to shared mutable state.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2024\/10217\/\">Explore Swift performance<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2024\/10217\/\"><p>Discover how Swift balances abstraction and performance. Learn what elements of performance to consider and how the Swift optimizer affects them. Explore the different features of Swift and how they&rsquo;re implemented to further understand the tradeoffs available that can impact performance.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2024\/10171\/\">Demystify explicitly built modules<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2024\/10171\/\">\n<p>Explore how builds are changing in Xcode 16 with explicitly built modules. Discover how modules are used to build your code, how explicitly built modules improve transparency in compilation tasks, and how you can optimize your build by sharing modules across targets.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/www.hackingwithswift.com\/articles\/269\/whats-new-in-swift-6\">Paul Hudson<\/a> (<a href=\"https:\/\/www.hackingwithswift.com\/swift\/6.0\">list<\/a>):<\/p>\n<blockquote cite=\"https:\/\/www.hackingwithswift.com\/articles\/269\/whats-new-in-swift-6\"><p>2024 is Swift&rsquo;s 10th anniversary, and for the last five of those years we&rsquo;ve had no major-version Swift updates &#x2013; literally half of Swift&rsquo;s life has been 5.0 through to 5.10.<\/p><p>This is more common than you might think. In fact, several major programming languages have some kind of release that takes significantly longer than all others: Python 3 took years to arrive, PHP 6 took so long the team bailed out and jumped straight to PHP 7, and Perl 6 dragged on so much that it ended up evolving into a different language called Raku.<\/p><p>Swift last had major breaking changes back in Swift 3, but when enabled in full Swift&rsquo;s own v6 has the potential to make Swift 3 look like a walk in the park. This is partly because of new changes, but partly also because many features added in recent Swift versions have been hidden behind feature flags that will be enabled by default in Swift 6.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/www.swift.org\/migration\/documentation\/migrationguide\/\">Migrating to Swift 6<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.swift.org\/migration\/documentation\/migrationguide\/\">\n<p>Swift&rsquo;s concurrency system, introduced in Swift 5.5, makes asynchronous and parallel code easier to write and understand. With the Swift 6 language mode, the compiler can now guarantee that concurrent programs are free of data races. When enabled, compiler safety checks that were previously optional become required.<\/p>\n<p>Adopting the Swift 6 language mode is entirely under your control on a per-target basis. Targets that build with previous modes, as well as code in other languages exposed to Swift, can all interoperate with modules that have been migrated to the Swift 6 language mode.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/rhonabwy.com\/2024\/06\/18\/my-favorite-swift-6-feature-static-library-compilation-on-linux\/\">Joe Heck<\/a>:<\/p>\n<blockquote cite=\"https:\/\/rhonabwy.com\/2024\/06\/18\/my-favorite-swift-6-feature-static-library-compilation-on-linux\/\">\n<p>There is a lot of  great stuff coming in the Swift programming language. I love the focus and effort on <a href=\"https:\/\/www.swift.org\/migration\/documentation\/swift-6-concurrency-migration-guide\/dataracesafety\/\">validating data-race safety<\/a>, and is probably the feature set that I&rsquo;ll spend the most time with. But my favorite new tidbit? Swift 6 now supports a Linux SDK and the ability to compile a stand-alone, statically linked binary.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/kean.blog\/post\/swift-6\">Alex Grebenyuk<\/a>:<\/p>\n<blockquote cite=\"https:\/\/kean.blog\/post\/swift-6\">\n<p>In recent years, there&rsquo;ve been some questionable changes, the latest one being <a href=\"https:\/\/www.swift.org\/migration\/documentation\/swift-6-concurrency-migration-guide\/dataracesafety\/\">Data Race Safety<\/a> in its current form in Xcode 16 beta.<\/p>\n<p>[&#8230;]<\/p>\n<p>If you want to migrate a large codebase to support Swift 6 mode, you need to fix thousands of compiler warnings that become errors once you enable this mode. For example, if you have any global variables, they are now errors.<\/p>\n<p>[&#8230;]<\/p>\n<p>In the ideal world, I would love to have more granular control over the types of warnings and errors the compiler produces, depending on what you can tolerate in your project. If data race safety is a compelling enough feature, people will enable it. There are also questions about whether it <em>should<\/em> be enabled by default and whether its current design can allow it to be enabled by default considering the lack of progressive disclosure.<\/p>\n<p>[&#8230;]<\/p>\n<p>Speaking about compile time, one of Swift&rsquo;s original premises was that it was &ldquo;fast,&rdquo; and you would expect it to apply to the compile time. However, with the current slow compilation, developers have to go to extreme lengths to work this around, including reinventing header files by creating protocol-only modules, which Swift was designed to eliminate. If there was a way to disable some of the language features to improve compile time, I would do it in an instant. I&rsquo;m bringing this up because I wonder what the impact of data race safety is going to be, especially once it gets upgraded with more advanced techniques for eliminating false positives.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/mastodon.social\/@a_grebenyuk\/112780580418057686\">Alex Grebenyuk<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@a_grebenyuk\/112780580418057686\"><p>For context, it took me months and multiple releases to get this ~4K lines of code somewhat compatible with Sendable and Swift 6. And I now have to revert my concurrency changes in Pulse from last week because I broke some stuff.<\/p><p>I&rsquo;m farily certain no existing large size codebase will ever be able to fully adopt Swift Concurrency Checking and Swift 6. I would also expect a short-term rise in concurrency bugs in iOS apps.<\/p><\/blockquote>\n\n<p>See also: <a href=\"https:\/\/qualitycoding.org\/conversation-swift6-data-race-safety\/\">Jon Reid: A Conversation With Swift 6 About Data Race Safety<\/a>.<\/p>\n\n<p><a href=\"https:\/\/mastodon.social\/@heathborders@hachyderm.io\/112779741678651536\">Heath Borders<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@heathborders@hachyderm.io\/112779741678651536\"><p>I really love Swift the language, but if I had my way, Twitch would only have switched to Swift 2-3 years ago when static linking was easily available.<\/p><p>I got a lot of pressure from other devs to switch back in 2018, so we did, but we had a modularized codebase in Objective-C that we had to demodulalize in Swift bc of no static linking back then. Our clean build times used to be 90 seconds, and now it&rsquo;s 8 minutes.<\/p><p>[&#8230;]<\/p><p>People would be mad if we were still in Objective-C, but I think that&rsquo;s bc they&rsquo;d see all the cool stuff at WWDC and wouldn&rsquo;t be able to use any of it. They&rsquo;d rightly think they were falling behind the industry. They wouldn&rsquo;t be as marketable in other jobs.<\/p><p>The main reason for Swift is because everyone else is using it.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/mastodon.social\/@helge\/112780925061878952\">Helge He&szlig;<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@helge\/112780925061878952\">\n<p>No, it is because of:<\/p>\n<p>&gt; fighting the direction of the platform, and fighting Apple is generally a losing game<\/p>\n<p>This is IMO not rooted in preferences of developers.<\/p>\n<p>A big issue here is that Apple ties features to the language (and unlike ObjC, Swift has no FFI). The Apple platform developer tooling is a closed system and Apple exploits that to lock devs into their own things over alternatives, regardless of quality.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/07\/17\/swift-testing-in-xcode-16\/\">Swift Testing in Xcode 16<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/07\/17\/xcode-16-announced\/\">Xcode 16 Announced<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/06\/04\/swift-at-10\/\">Swift at 10<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/03\/06\/swift-5-10\/\">Swift 5.10<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/11\/28\/swift-proposal-typed-throws\/\">Swift Proposal: Typed Throws<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/11\/07\/swift-proposal-pack-iteration\/\">Swift Proposal: Pack Iteration<\/a><\/li>\n<\/ul>\n\n<p id=\"swift-6-announced-update-2024-07-23\">Update (2024-07-23): <a href=\"https:\/\/mastodon.cloud\/@drewmccormack\/112834944446093987\">Drew McCormack<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.cloud\/@drewmccormack\/112834944446093987\"><p>It is a risky time for Swift. They are channeling enormous resources into solving multi-threading at compile time, but the remedy is worse than the sickness. They are introducing a whole different problem: systemic race conditions. Interleaving of async functions. These are much more difficult to track down IMO. I am literally breaking my head on some of these things. Without a transactional system like dispatch, you end up with something more complex than multithreading.<\/p><\/blockquote>\n\n<p id=\"swift-6-announced-update-2024-08-08\">Update (2024-08-08): <a href=\"https:\/\/mastodon.social\/@cocoaphony\/112893796368067518\">Rob Napier<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@cocoaphony\/112893796368067518\">\n<p>Every time you write Task, I want you to pretend it's actually this:<\/p>\n<pre>Task {\n    let delay: Int = (0...10).randomElement()!\n    try await Task.sleep(for: .seconds(delay))\n\n    \/\/ .. Your code\n}\n<\/pre>\n<p>Is your code still correct? I not, then you need to make it correct.<\/p>\n<p>Tasks do not make promises about when they start. Currently, they do not even make promises about whether they start in order (they will, but not quite yet). So if the above addition would break your code, your code is wrong.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/mastodon.social\/@cocoaphony\/112894071780181027\">Rob Napier<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@cocoaphony\/112894071780181027\"><p>IMO, no one today understands Swift Concurrency in depth, not even the core team. We&rsquo;re all kind of feeling our way through it together in public and hoping to discover patterns that are more correct than what we were doing before. There are many great resources for the basics out there, but I don&rsquo;t think there&rsquo;s any one resource for how to use this tool &ldquo;the right way&rdquo; because I don&rsquo;t think anyone knows that yet.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/mastodon.cloud\/@drewmccormack\/112920657166251155\">Drew McCormack<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.cloud\/@drewmccormack\/112920657166251155\"><p>The thing about the Swift 6 concurrency bomb is that I know already it won&rsquo;t catch a single bug. Every error I have to fix is purely to appease the compiler. I see in every case that my code logic is correct, that there is no concurrent access. I also don&rsquo;t see crashes related to threading (at least not in app level code). Like most static checking dogma, you end up spending a lot of time doing busy work, purely to tell the compiler what you already know.<\/p><p>The irony is that a lot of the solutions to the Swift 6 concurrency errors are to introduce another layer of indirection in the form of an async func. I think it may actually lead to new bugs, rather than fixing old ones. Wouldn&rsquo;t surprise me if we see problems due to interleaving or data races that weren&rsquo;t there before.<\/p><\/blockquote>\n\n<p>See also: <a href=\"https:\/\/x.com\/krzyzanowskim\/status\/1821277766639358269\">Marcin Krzyzanowski<\/a>.<\/p>\n\n<p id=\"swift-6-announced-update-2024-08-13\">Update (2024-08-13): <a href=\"https:\/\/mastodon.online\/@tclementdev\/112944607961736299\">Thomas Clement<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.online\/@tclementdev\/112944607961736299\">\n<p>Apple encouraged developers into updating all their completion hander based functions into async functions which suddenly caused them all to move off the main thread. It was before the concurrency warnings and in the last few years it&rsquo;s been the number 1 cause of crashes in the projects I&rsquo;ve been working on. Yes there&rsquo;s quite a lot of it.<\/p>\n<\/blockquote>\n\n<p id=\"swift-6-announced-update-2024-09-23\">Update (2024-09-23): <a href=\"https:\/\/crunchybagel.com\/a-strategy-for-moving-to-swift-6-and-async-await\/\">Quentin Zervaas<\/a>:<\/p>\n<blockquote cite=\"https:\/\/crunchybagel.com\/a-strategy-for-moving-to-swift-6-and-async-await\/\"><p>In the iOS 18 \/ Xcode 16 development cycle (2024), I&rsquo;ve converted a lot of my code from using completion handlers to using async\/await.<\/p><p>This article outlines the general strategy I&rsquo;ve used, which has been quite effective.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>What&rsquo;s new in Swift: We&rsquo;ll briefly go through a history of Swift over the past decade, and show you how the community has grown through workgroups, expanded the package ecosystem, and increased platform support. We&rsquo;ll introduce you to a new language mode that achieves data-race safety by default, and a language subset that lets you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2024-07-22T18:15:52Z","apple_news_api_id":"bcf9f35d-a4f6-419b-a8c6-a5800215e2b2","apple_news_api_modified_at":"2024-09-23T18:05:19Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAABA==","apple_news_api_share_url":"https:\/\/apple.news\/AvPnzXaT2QZuoxqWAAhXisg","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,71,2200,901],"class_list":["post-44177","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-concurrency","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=44177"}],"version-history":[{"count":6,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44177\/revisions"}],"predecessor-version":[{"id":45056,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44177\/revisions\/45056"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=44177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=44177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=44177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}