6 ms·
Swift 6 is amazing - I say this as a big Rust person. It seems like Swift is now its own entity outside the Apple bubble and has a few very interesting features
by jkelleyrtp 2y ago
Swift 6 is amazing - I say this as a big Rust person. It seems like Swift is now its own entity outside the Apple bubble and has a few very interesting features:
- An "embedded" mode that turns off reflection for kilobyte-sized binaries
- An upcoming WASM target
- An LSP for VSCode
- Native C++ interop
- Typed "throws"
- Static linking on linux and the linux sdk
- Porting a number of "core foundation" / foundation primitives to nix platforms
- Distributed actors for concurrency that run on distant machines
- Data-race free by default
- non-copy RAII generic types
Watch the video on Swift 6: https://developer.apple.com/videos/play/wwdc2024/10136/ https://developer.apple.com/videos/play/wwdc2024/10136/.
IMO Swift 6 is a better Go and than Go, and a better Rust for app-dev type stuff, which seems to be what people want to use Rust for.
Swift has shipped stuff that Rust has been sitting on its hands over for years:
- Anonymous enums/structs in parameters/match statements
- Named function arguments
- A type of `Send` bound that allows Rcs to be sent between threads (not Arcs)
- Swift preview hotreloading
- autoclones (without GC)
Swift also took a bunch of goodies from Rust *[1]
- No garbage collector
- Traits via "protocol"
- ADT for Option/Result
- macros
- non-copy types enforcing RAII
I really really wish Rust had been keeping up with Swift's ergonomics because it's looking like Swift is going to catch up to Rust's cross-platform story much faster. Swift really could end up as the "do it all" language for frontend/backend/app dev.
[1] By "took" I mean if you like these things in Rust you'll see them in Swift
- christophilus 2y agoHow’s the compilation speed?If it’s going to replace Go, that’s gotta be sub second for a medium sized project on my clunky old laptop.
- coldtea 2y agoWhy, would you care if it's a full second or even 5 seconds? Go hyped their compilation speed a lot in its early years, but unless it's C++ templates level slugginess it's not like it's that big of deal.
- square_usual 2y agoCompiling fast has ripple effects throughout your workflow. Having your iteration time go from 1s to 5s is a massive difference: it can mean you break out of the flow state when you restart your server to test your code.
- manmal 2y agoAs someone dealing with Swift everyday (and, before that, faster langs like Elixir and Typescript), I can only agree. Sometimes my incremental builds take 20-30 seconds and it feels like a huge toll. Waiting on the CI (which is a weak machine) is up to 25 minutes until the test suite has passed - a large chunk of that being compilation time. Releases take 5-10 minutes. I really like Swift, but I’d highly appreciate faster compilation times.
- iainmerrick 2y agoOnce you use a language where compile times are essentially invisible, it’s hard to go back.
- plorkyeran 2y agoSwift compilation is slower than C++.
- coldtea 2y agoC++ with heavy template use or C++ as glorified C-with-classes?
- plorkyeran 2y agoC++ with heavy template use.
- jkelleyrtp 2y agoSupposedly faster in Swift 6 but I don't have any big swift projects to throw at it. It uses LLVM so it's not going to be as fast as Go's. In Swift 6 they: - Improved parallelism via compilation pipelining (ie top-level object files are referencing not-yet-compiled symbols but can still proceed while those compile in the background) - Parallelized and pipelined debug symbol generation (lazy debug symbols)
- josephg 2y agoAssuming my experience with Swift 5 is still accurate, compilation speed is at least an order of magnitude slower than Go. It’s a much more powerful language. In my opinion slower compilation is worth it for all of its great features. (Does Go even have parametric enums?). But its definitely a trade off.
- MBCook 2y agoThat’s the one big loss from moving off Objective-C. Because the language was so stupidly simple to parse and didn’t have a lot of “we’ll figure that out for you from what you wrote” it could be compiled incredibly quick on today’s machines. I know they’re working on improving it. I’m glad they are. But that speed is something I definitely miss.
- christophilus 2y agoOcaml makes me wonder if we really do need the trade off. It’s a fairly advanced language, and compiles super fast. I wonder why it’s such an outlier among advanced languages?
- coldtea 2y ago>Swift also took a bunch of goodies from Rust - No garbage collector - Traits via "protocol" I'd say they took them from Apple's own Objective-C additions who had those for decades before Rust (ARC and protocols).
- e63f67dd-065b 2y agoI'm primarily a C++ developer these days, and if the C++ interop is actually good then I'm very excited for the future. For app-dev type stuff it really does seem like it's better than Rust, and I can definitely see a world where we start migrating some code into Swift. Edit: back from watching the video, and a few observations: - Swift does not suffer from Rust's refusal to add things into the standard library. gasp regex support in stdlib? Async/await runtimes? - Default testing framework is a godsend, especially when integrated into the build system - Rust but better for everything that's not low-level systems development is the impression I get
- jkelleyrtp 2y agoThe default testing framework made my jaw drop a bit - I have a very hard time seeing Rust have the enthusiasm or momentum to implement something like that right now.
- glhaynes 2y agoI'm not a (direct) user of the C++ interop, but everything I've heard about it has been quite positive. A few hopefully relevant resources, first a couple of videos: - "Introducing a Memory-Safe Successor Language in Large C++ Code Bases" (John McCall; CppNow 2023) https://www.youtube.com/watch?v=lgivCGdmFrw https://www.youtube.com/watch?v=lgivCGdmFrw - "Swift as C++ Successor in FoundationDB" (Konrad Malawski; Strange Loop 2023) https://www.youtube.com/watch?v=ZQc9-seU-5k https://www.youtube.com/watch?v=ZQc9-seU-5k And then a series of blog posts by Doug Gregor: - "Swift for C++ Practitioners" https://www.douggregor.net/posts/ https://www.douggregor.net/posts/ And finally: - "Mixing Swift and C++" (Swift.org) https://www.swift.org/documentation/cxx-interop/ https://www.swift.org/documentation/cxx-interop/
- dagmx 2y agoAs another huge rust fan myself, Swift 6 has really won me over. I find myself reaching for Swift as the default for a lot of things rather than Rust, especially with the advent of C++ Interoperability and the new Swift embedded stack and ownership model. The only thing I really need is a UI story on other platforms, but it’s an area Rust is lacking great solutions too. I suspect the C++ interop might eventually make Swift have great Qt bindings to compete with PySide as an accessible way to make multi platform UIs.
- jclay 2y agoThere’s a very minimal example using C++ interop in Qt’s test suite: https://github.com/qt/qtdeclarative/tree/dev/tests/manual/helloswift https://github.com/qt/qtdeclarative/tree/dev/tests/manual/he... I’ve kicked around with the C++ interop a few times. Projects like Qt that use raw pointers extensively seem hard to expose. Ended up having to wrap Qt classes to model as a value type which was straight forward but it’s not nearly as seamless as the Objective-C++ experience
- dagmx 2y agoAh, very cool. Thanks for the link and write up
- square_usual 2y agoSwift and Go are on polar ends of the language complexity spectrum. Someone who enjoys Go for what it is, i.e. a dead simple, straightforward language, would not see Swift as being "better".
- jkelleyrtp 2y agoSome people want generics and ADTs in Go. If you're using go because it's 1) fast 2) expressive 3) concurrent 4) compiles natively then I think Swift is the better of the two. If you just want a very simple language, then sure, Go is there. But I don't think that's necessarily what makes Go, Go.
- beanjuiceII 2y agoafter using swift extensively i did not find it better of the two, in fact it is a complex keyword ridden language that seems bolted together at very many angles, each swift codebase i enter is like a new mystery to unfold because of its extremely rocky evolution
- Klonoar 2y agoOne thing that bugs me to this day is reading Swift code without an IDE can be an exercise in “let me build up a mountain of context first”, due to the way enums and types can get shortened. It’s nice to write but a pain to review. Meanwhile if I read Rust or Go, it’s more or less what you see is what you get. I do agree in general that it’s frustrating that Rust hasn’t kept up with some of Swift’s pushes, but then again that’s the difference when you have the richest company in the world involved. For context, I’ve written many apps in Swift, deployed a backend in Vapor, and worked with teams who use the language. I always thought I’d get over it but it just never fully clicked.
- msie 2y agoYes! Reading Swift code, even tutorial code, can be difficult.
- square_usual 2y ago
- deleted 2y ago[deleted]
- cube2222 2y agoAgreed that Swift looks quite amazing. There’s some cool talks on how they’re trying to move FoundationDB to Swift and their approach. I think the main problem, and this is a really big problem, is that based on a very shallow investigation I did a couple months back, the ecosystem for server-side stuff barely exists. And in my opinion, the ecosystem is one of the deciding factors when picking a language.
- jkelleyrtp 2y agoI have a slight inkling that Apple internally is using Swift for backend stuff somewhere, especially with all the ML stuff they just shipped. Word on the street is their datacenters are full of M-series chips. I could see those servers running Swift + distributed actors + mlx. The move to make swift run on Linux could be part of that work - their sysadmins, devops, and backend folks probably use Vim/VSCode and deploy on Linux. We might see the release of a very good backend framework by apple for Swift that works on Mac/Linux. The new macros they added kinda bolt on serde into the language too - and the distributed actor stuff is basically RPC in the language.
- manmal 2y agoYes their “private cloud” runs on Swift, which supposedly increases security because they believe Swift has things nailed by now.
- Klonoar 2y agoI believe it’s confirmed that they’re doing it with Swift, buried somewhere in the ML post IIRC.
- MBCook 2y agoApple deploys a ton of their server stuff on Linux. Plus they appear to be doing all new code in Swift and moving old code over slowly. They’re also using embedded Swift in the Secure Enclaves and other small processors due to its safety, though I think they have an extra layer of automated security checking that they use internally that doesn’t exist in the public builds. But in short, yes. I believe they’re trying to use Swift for everything at this point and getting away from C/C++/whatever even for kernel work.
- KurtMueller 2y agoAlgebraic data types are not a Rust invention and aren't exclusive to that language.
- MBCook 2y agoI don’t think they were trying to claim that, just that it was a feature that was in Rust that they liked and wanted to keep using. Just like Rust didn’t invent not using garbage collection.
- mdhb 2y agoYou almost caught up to Dart. - Built for embedded devices and already has RISC-V support - Already has WASM was a compilation target - Not just an LSP but amazing VSCode tooling - Native C, C++, Java, Rust and JavaScript interop and a .NET bridge is in the works it seems. - Typed "throws" - Static linking (in development) - Data-race free by default Unique features to Swift: - Distributed actors for concurrency that run on distant machines. (This is doable but not native to the language) - non-copy RAII generic types (not actually sure what this is so I assume it’s not in Dart)
- almostgotcaught 2y ago> - Native C, C++, Java, Rust and JavaScript interop dart doesn't do C++ interop
- manmal 2y agoIsn’t “data-race free” in Dart achieved by just not really sharing mutable memory between isolates? Probably because the web version needs to map isolates to web workers, right?
- mdhb 2y agoFor now but this is in the pipeline too. https://github.com/dart-lang/language/blob/main/working/333%20-%20shared%20memory%20multithreading/proposal.md https://github.com/dart-lang/language/blob/main/working/333%...
- manmal 2y agoGreat writeup, and I love that the authors looked at a lot of other languages. The solution section on how to prevent races is a bit light still - I‘m curious how they will solve this. I think there will need to be exceptions to a strict race-free model eto not take away from dart‘s current capabilities, and without forcing people into something like a borrow checker. Swift has `@unchecked Sendable` which lets you tell the compiler "I promise I'll take care myself to keep everything in there race-free". What I‘ve been missing in Swift are tools that let me add safety _within_ such @unchecked Sendables. There's the Swift Atomics lib: https://www.swift.org/blog/swift-atomics/ https://www.swift.org/blog/swift-atomics/, but its quite limited in its capabilities. So for now, I find myself reaching for tools provided by the community, like pointfree's LockIsolated: https://github.com/pointfreeco/swift-concurrency-extras/blob/main/Sources/ConcurrencyExtras/LockIsolated.swift https://github.com/pointfreeco/swift-concurrency-extras/blob....
- deleted 2y ago[deleted]
- heavyset_go 2y agoGiven there is native C++ interop, is exporting a C ABI still a seemingly experimental/niche feature? Same question with interop between C and Swift. Recently went with using Kotlin Native over Swift on macOS for this reason, the C ABI export and C interop were painless.
- MBCook 2y agoSwift has painless interop with Objective-C, which is just C plus small talk. C should already be pretty easy, though I haven’t really used it myself. I only use the Objective-C stuff. I know they’ve been working on C++ but don’t have any experience to comment. Someone else had a nice comment listing resources on that.
- mannuch 2y agoSwift has had first-class C interop for a long time. It is only C++ interop that is more experimental/in-progress.
- vips7L 2y ago> - No garbage collector ARC is a GC, and a slower one than most traditional tracing GC's.
- MBCook 2y agoReference counting and garbage collection are different. They’re both forms automatic memory management, but they work in very different ways.
- vips7L 2y agoNo they're not. Reference counting is a garbage collection algorithm. Its even described as such in most literature on garbage collection.
- kibwen 2y agoReference counting and garbage collection are different, yes. But in terms of classifying "no garbage collection" under "things that Swift took from Rust", Swift's pervasive reference counting is much closer to classic tracing GC than it is to Rust's system of single ownership.
- pcwalton 2y agoAutomatic memory management is a synonym for garbage collection. Reference counting is a form of garbage collection.
- mannuch 2y agoFirst of all, traditional (tracing) GCs require more expensive runtimes and consume much more memory. ARCs are also much more predictable in their behavior and pave the way for greater compile time optimizations. ARC is key in providing Swift its ability to maintain its low memory footprint compared to, say, Java. Second, yes, ARC is a GC method. But when the language says "no garbage collector", they just mean no traditional (tracing) GC runtime because, for better or worse, tracing GC is what people think of when they hear "garbage collector".
- 2y ago
- lll-o-lll 2y ago> No garbage collector No tracing gc. Ref counting is the other kind of garbage collector.
- stux 2y ago> Anonymous enums/structs in parameters Can you share an example of this? It sounds really interesting but I couldn’t find any references. Do you mean the parameter “packs” features?
- wg0 2y ago> IMO Swift 6 is a better Go and than Go, and a better Rust for app-dev type stuff, which seems to be what people want to use Rust for. This is a great assessment. There are some shortcomings in go that I wish weren't and I'm kind surprised that people having decades of language design experience under their belt ended up designing go. I'm looking for a way to generate server stubs from OpenAPI specs. If that, I'm all set with Vapor+OpenAPI and would retire go for my container adventures.
- glhaynes 2y agoI'm looking for a way to generate server stubs from OpenAPI specs I don't have experience with OpenAPI, but is this what you're looking for? https://www.swift.org/blog/introducing-swift-openapi-generator/ https://www.swift.org/blog/introducing-swift-openapi-generat...
- wg0 2y agoYou're right. This actually is first class Apple[0] and can't get any better than that whereas with Go, I am stuck with so many third party packages. They work well but kinda... not so elegant. Decent enough given what the language can offer. [0] https://github.com/apple/swift-openapi-generator https://github.com/apple/swift-openapi-generator
- pcwalton 2y agoSwift has a garbage collector. Reference counting is a form of garbage collection.
- BodkinsOdds 2y ago- Anonymous enums/structs in parameters/match statements I'm not sure what you mean by this, could you elaborate?