98 points · by todsacerdoti · 4y ago · 99 comments
Swift’s focus on progressive disclosure means most Swift users won’t need to worry about this.
Unfortunately for me, it's not aligned with my preferences. I like small languages and frameworks that I can master and build upon. Tough luck for me, of course, who cares.
But I'm not convinced Swift is also aligned with what should be the language's main purpose: build great iOS and Mac apps.
8 years later, it's safe to say it's not the language of choice for the new kernel or driver code, not the one for the hot new web framework and it's not replacing Python.
The only thing it's got going for it is that it's the only officially supported choice for making iOS apps going forward. Ultimately, no one uses Swift voluntarily, and that's a bad sign.
I write all of this with sorrow, I was cheering with joy when Swift was released.
Sorry but this can't be more wrong. I love Swift so much that I can tolerate and forgive its compiler's sluggishness.
Swift is now available on Linux (has been for a while) and there's Apple's server framework called SwiftNIO. It's designed for fast and efficient multithreaded server-side code. I voluntarily picked it for my next backend project and am very happy so far.
I would use it in more places if I could, it's a language I feel extremely productive in. Apple however has shown zero interest in helping popularise it anywhere outside of their ecosystem and left it up to the community.
I write Go for work and my all-time favorite language is probably still Rust, but I find that a ton of really thoughtful and smart choices have gone into Swift-the-language as well as how it’s used to build apps.
Happy to expand more on what I like about using Swift if you’re curious.
[1] which you can now write as the same codebase using SwiftUI
They also dedicated some time on the state of the frameworks session, to put it clearly out there that Objective-C is done and it is time to move on.
It took about 20 years for C++ to take over some key areas to C, and even there it wasn't a success in POSIX and embedded, where C still rules.
8 years isn't nothing.
Rust has become all that, and for a change, I am happy to observe that we're stuck with a pretty good language.
Not true for me, I've used it successfully for server side work. Being able to use the same language is nice since I can pull in the same routines/data structures and not re-implement them.
Sounds like D.
Instead we got Swift, which ironically, given its uptake by external devs, I think was aimed at complaints from internal Apple users. Internal users at Apple were frustrated by how easy it was for external devs to introspect and modify private ObjC framework code. They also wanted performance features like value types and method inlining.
I don’t think many external app devs cared about those things, or to the extent they did C++ was an attractive option because a stable ABI doesn’t really matter internal to application code.
I continue to use ObjC for Mac and iOS dev because it’s a much simpler language than Swift, the system APIs I interact with are still implemented in ObjC, and C++ interop is easier via ObjC++. I suppose the day will come where I need something that is only accessible from Swift, and I’ll write Swift then, but for now Swift doesn’t solve any problems that I have.
Swift apps are not crashing randomly, you don't have nils running rampant because of type safety.
You can create types that nicely convey reality using rich enums and you don't have to define objects with 18 fields that are mostly always nil, but when propertyA == 16, then this field has some value.
Swift also is not an official superset of C, like Objective-C, so you don't have all the footguns of C language, like integer promotions and assignment being an expression.
You just have so much improvements over objective-C if you want to ship an app to hundreds of thousands of users and be sure that it works correctly and that unhandled edge cases won't blow up.
Maybe it is more complex to write a compilable piece of swift, that a compilable piece of Objective-C, but I am happy that this is the case, because it means that the compiler has my back and finds bugs and issues for me.
With the features which were released alongside SwiftUI, it seems like that went out the window, and Swift pivoted towards being a DSL for iPhone apps.
Rust has its macros, Swift has its result-builders.
The latter wins a nice way to declaratively-describe UI widgets, but if that's not what I'm doing I don't need to think about it.
I wish there are more languages that focused on happiness of the compiler authors so that they can write efficient and small compilers with well-thought language features that intentionally limits how much they need to write in the compiler. Today we have such a huge compiler maintenance burden and as a result, you saw inconsistencies everywhere with these languages, and it is after many hundreds of person-year spent.
(Yeah, Go is probably one of the language that focused on compiler authors' happiness)
It has been on Swift documentation from day one.
But read/listen to what Lattner says and you get a different take.
It seems like a lot of the iOS veterans are still not interested in switching to it so who knows, maybe I'll come back to that in the future when it's actually production-ready like Apple claims it is.
Also I found that once you get into stuff like animations, things get messy very quickly since everything has to be expressed declaratively. It's a nice idea for simple static views, but I really missed the ability to have separation of concerns the way you can with UIKit.
Also I did find the tooling quite buggy. Like certain things would break the preview, and not give meaningful errors or just crash the Xcode process, so I would have to fix it through trial and error with commenting out sections of code until it would work again.
I am sure it will improve, but with UIKit you can basically achieve anything, and SwiftUI still seems like it has a lot to figure out.
I think it's a shame FRP has basically won front-end at this time, because while it's been a revelation in web (since it solves a lot of the problems which make web a mess) it carries a lot of its own problems, and I think there are going to be a lot of apps which will be horrific to maintain over the next few years.
SwiftUI has it's issues and one is platform support. If you want to support anything before iOS 13 (even that has limited support) then you are out of luck. The only SwiftUI I have shipped is for WidgetKit and WatchOS, both being somewhat forced upon you.
While it can be cumbersome to do simple things in SwiftUI that were easy to do in UIKit, I haven't found too many bugs. Most issues I have run into are on the Xcode side.
If you work with any Apple platform it is worth pursuing. If you want to just learn and use Swift it does have decent support on Linux thanks to being able to bind with C libraries. Windows support is there, but is very much in its infancy.
"copy on write" value semantics mean you can have two variable references to the same memory for a long time before one mutation causes the space to be copied and mutated, leaving the other unaffected.
The function name "move" here is beyond terrible. It makes sense only to compiler writers, and even then it is ambiguous.
The function's goal/effect is to end the lifetime of a variable binding, to essentially force the COW.
(So much for scoping.)
More generally, Swift grows in fits and starts mostly driven by Apple internal needs. So if Apple needs `move(x)`, you might, too.
Poor Swift team: fully exposed in open-source, but driven and locked to Apple's needs. All the best to them.
These are exactly the sorts of issues that an evolution proposal is supposed to shake out. It gives users a chance to weigh in on new features, including naming, before they land in the language or standard library. The name is very much not finalized yet, and comments pointing toward a better binding for the functionality would be welcome.
It's a fairly fundamental change, though. I'm not a compiler person, so I don't know how much cheese it would move. It also may argue with some of the built-in optimizations.
If you want to see these in action, run the symbolic debugger in release mode.
However, the scope creep of Swift is becoming nightmarish - nothing just makes sense anymore.
My hopes for Swift are that it teaches a whole generation how to program really compelling applications, full stack, in a profitable ecosystem (though open source, Swift is a 'gateway drug' to get people into the ecosystem). We will see if these decisions advance or detract from that.
That said I really wish the tooling would keep up. Debugging stuff like SwiftUI and async/await code is really painful right now. It feels like they keep adding this stuff without updating the tools we need to have available to really support it. This is in stark contrast to what was/is available for Objective-C and UIKit with the debugger and instruments. I'm not sure it's a good tradeoff... The high level code becomes a lot easier to read and write but keeping a mental model of what's happening is a lot harder and it becomes very thorny when things go sideways.
There's an extremely stark contrast between Xcode and Android Studio any time you need to do refactoring, for example. Xcode's refactoring tools are both limited and very very finnicky (to the point of being basically useless).
It has not always been fun. Lots of atomic wedgies, in the recess yard, but things have worked out OK, in the long run.
I tend to declare many of my object references as weak, anyway; even if not necessary.
Of course, I'm old-fashioned (and I primarily work with UIKit), so I use classes a lot.
If I am doing it the "current" way, most of the program uses value semantics, so it should not be an issue (in theory, anyway).
Why?
Actually it's because approximately every damn compiler framework or textbook assumes that local variables go into and out of scope in a stack/LIFO order. (So if you declare `foo` and then `bar`, `foo` can't go out of scope until `bar` does, even if `foo` was a random temporary and `bar` is needed for the entire rest of the function.) And all the algorithms for working with local variables bake in this assumption, so you have to redesign everything to get back the sane semantics you get for free in assembly.
At least that's my experience with implementing proper local variable scoping; YMMV.
Fortunately, these days languages that are imperative at their core pick up "functional spillover" like that kotlin let seem to solve that nuisance-grade problem of a dangling `foo` temp just fine.
(the kotlin let is basically a list transform applied to a single instance, without all the pointless ceremony of wrapping as an Optional. I wanted to like scala more than kotlin, but I've come to respect kotlin a lot, for striking a balance between "language designer high" and the pragmatic mundane that must never be allowed full reign or else we get another groovy)
Actually, runtime does not use a stack in the relevant sense (on non-toy compilers anyway). It allocates a stack frame on the call stack on a per-function basis, with possibly some other bits to handle variable-length arrays, but the actual local variables are allocated inside the stack frame in mostly the same way they're allocated in registers (but with less spilling). So the same 'undeclare'/register-coloring semantics apply.
It's true that this is a bunch of magic, while Rust's drop() is so non magical its implementation, which you could copy-paste if you wanted, is supplied in the documentation:
pub fn drop<T>(_x: T) { }
But the reason Rust programmers write drop(x) is going to be much the same as this proposal would have Swift programmers write _ = move(x) -- to explicitly tell the compiler they want to destroy x right here. So I think as a programmer the drop(x) would make sense.This works exceedingly well. It essentially maps to what I want to do in almost all cases: Things that can trivially be copied (because they don't contain a resource) will usually work as you'd expect in any other programming language, while things that cannot (because they contain a resource like a file descriptor, or an owned pointer to some allocated memory) will indeed mark the old variable as unusable.
This is a really pleasant model to work with. So much so that I miss it terribly whenever I work in another language.
But I guess it didn't fit to migrate from current obj-c ecosystem so they left it for later.
it should be built in
std lib means you'll have to import a module
import Move
move(a)Even something as simple as breakpoints and printlns let me inspect the intermediary state of these systems.
After that, your issues then are typically reference cycles, which can really only be found by tooling at runtime or by certain linting tools (e.g. warn that the type definitions make reference cycles possible, even if your code isn't making cycles). There are tools such as Instruments included with Xcode to help detect cycles at runtime.