Embedded Swift [video]
youtube.com
youtube.com
I really enjoy programming in Swift. But I’m not convinced it’ll ever escape the confines of iOS development because there isn’t that persuasive a case for it. It’s nice. But “nice” isn’t enough motivation to create an entire ecosystem. You’re not going to be able to reuse most iOS-oriented swift code in an embedded system so you’ll be starting from scratch.
Just look at backend web development. There's prodigious duplication of effort by each language camp to create their own web framework, database libraries, etc. Why does each camp bother? They just think their chosen language is really nice.
Your high level application gets to be in swift and create objects, I assume have concurrency, a robust error and exception system.
I like it. I’m not sure it’s going to beat Rust or Zig, but I wouldn’t be upset if it did.
I would seriously look at it for the next project. I love that you can keep VS Code, CMake, and dockerize the environment. What I’m not sure about is the debugging.
This kind of embedded stuff is also used internally at Apple, and is preparing the work to replace firmware stuff like iBoot that is written on a Apple specific Safe C dialect.
Swift is probsbly to late to the party considering big competition amount languages and their ecosystems being "nice" today is not enough.
The ease of the language and the self-contained binaries just changed how fast that could happen, not whether it would happen.
Google hype, popular cloud native open source projects showing its potential, well known creators, good to acceptable tooling, strong standard library, low memory footprint, concurrency, simplicity, productivity, the whole philosophy behind it.
Some of these are questionable, but I heard all these by some people.
What does this say about Swift's chances to gain mainstream popularity outside of the Apple ecosystem? Probably nothing, as the languages and the circumstances are so different.
* linux, windows and wasm compilation targets. They hired the maintainer of wasm project as well. * LSP language server for Swift, to power VSCode and others editors, even though, they use Xcode that doesn't need it * well tested server side libraries similar to netty/jetty (they even have a former Akka developer)
This potentially results in a big fragmentation of the ecosystem. For instance, even though “embedded C” as a language is really just C, the vast majority of C libraries cannot be used on embedded systems because they’ll use the occasional malloc, mmap or fopen call.
Rust is particularly good at this, with its [no_std] directive that is well documented and used in a wide variety of libraries.
I wonder how Swift plans to address this.
Very often dynamic memory allocation and file system access are explicitly forbidden from an embedded project. They can both cause security problems and unpredictable behavior.
This is where they implied that operating system level functionalities are not found on computers without operating systems.
The fragmentation that exists in embedded is due to the nature of embedded systems, not programming languages. PCs, laptops, and smartphones are general purpose computers. Just about everything else is an embedded device, hardware custom designed for a specific purpose, given just enough resources to accomplish its intended task. Writing software for a general purpose computer on top of a full OS can largely be done without caring about the underlying hardware. Writing embedded firmware without an OS is only possible through understanding the specific hardware you are targeting.
I recently set up an embedded-hal project for cortex-m and after some struggle, I was happy to get dynamic memory allocation and std working but the platform had a tier 2.5 support so I had to put [restricted_std] if I wanted to use it.
But that means none of your dependencies can use std unless they also declare [restricted_std]. So now I have the stdlib but none of my dependencies can use it.
Swift subsets the language and libraries (which the compiler inlines). No complicated strings or runtime metadata for generic existentials.
And new features for ownership. Seamless C interop.
There's a lot to it!
Given engineers' skills at Apple using Rust would not be an issue.
I like to think about it like this: Rust forces you to think about memory where Swift doesn’t.
But what’s really cool is that Swift is adopting some of the great things about Rust’s ownership system, allowing you to “opt-in” to a Rust-style performance profile if you think the mental overhead is worth it for your use-case or domain.
To see what I’m talking about check out https://youtu.be/I9XGyizHxmU?si=Zl-7dA0NqhnctRPQ
What’s exciting to me is that Swift is becoming a great language that works at all levels of the stack while staying a language that feels really great to write.
IMO what’s largely held it back in adoption is its cross-platform experience, but you see that the Swift team is really trying to change the perception here (they’ve specifically tried to highlight using non-Xcode/non-Mac dev environments, deploying to Linux, developing embedded etc.).
This is not the first Embedded <LANG> attempted. For example, Embedded C++ [1] has been around for a couple of decades and is only used in a few weird places like Apple Device Drivers.
The problem is that if you are so very constrained that you can't afford to have dynamic memory that is reference counted, then you probably have so little memory and functionality that you may as well just use C as an advanced assembly language for the processor.
https://tockos.org/ and https://oxidos.io/
https://embassy.dev/ is pretty popular too.
note: even apple engineers hate Xcode, +1 for neovim (https://youtu.be/LqxbsADqDI4?feature=shared&t=229)
Just like when Microsoft started doing .NET demos on Mac laptops with VSCode, to send the message across.
[1]: https://github.com/apple/swift-embedded-examples/tree/main/p...