184 points · by glhaynes · 8y ago · 139 comments
The only reason I suspect that Apple is not all over Swift is that many of their programmers are the type of coders who like falling back to C when they feel they need a performance boost. Perhaps they like the message-like nature of ObjC. Dunno. I can say that I've been able to get apps to 0 fixable crashes with Swift (some crashes are system level), not something I've ever done with ObjC, let alone Java for Android.
Having worked on a large Obj-C codebase that began adopting Swift, there are definitely some headaches involved in Obj-C/Swift interop, and you often aren't getting many benefits of Swift unless your new code has few to no dependencies on / is not depended on by Obj-C code, unless you can refactor it into Swift.
Given how quickly it seems they've been forced to ship, I can understand not wanting to deal with these issues, or not having the time to address them.
- It was easier to hire developers
- Code was less prone to null pointer exceptions
- There were fewer assumptions when deserializing JSON into a struct/object
Not saying that it's an unsolvable problem elsewhere, it's just an anecdotal benefit we got
It's weird using Swift and realizing how much of that trouble we went through is avoided just by not using a GC'd language
Question for anyone who knows: what percentage of Apple's application-software programmers double as system-software/kernel programmers? Because I expect that'd color their preferences for a daily-driver language quite a bit.
I've developed since for version 1, small utilities, and even for those projects, it was not enough.
Version 4 and it's my favorite language to develop for.
In Swift, this is currently not stable, meaning it changes with each version. The result is that you can't use a library compiled with version X of Swift with version Y of Swift. From version 5 onward the ABI will be compatible between version 5 and versions >5.
More likely the language just matured. I'm glad they were not afraid to iterate as it is hard to get everything right on the first try and being stuck with a suboptimal design for the next 20 years would have been much worse.
This is, in many ways, the diametric opposite of what this thread is discussing: pushing out nominally "stable" versions to the outside world, breaking things, but not committing internally.
https://www.reddit.com/r/swift/comments/8zb9y1/state_of_swif...
For that, I'm exploring with rust now, but certainly is far harder than use swift.
I also look at nim, yet I think is too inmature for use in ios/android?
Swift is a great language to work in.
By doing optional chaining you can do something like: if let name = result.person?.firstName { ... } which only evaluates firstName if person is not nil, and then stores the result in name which is immutable.
It's easier to avoid mutable code in Swift than in many other languages. For this reason I would love to start working with Swift on the backend at some point in the future.
I like extensions, being able to extend any class I want by just typing it into any file I want. This helps with iterative development and trying something out.
Speaking of iteratively, if I want to try something quick, I can also drop down to a shell and start the Swift REPL to experiment.
I like how built-in functions like zip and mapValues are available, so it in some ways has a Pythonic feel. I like the syntax of building strings by just taking a variable name and going let s = "Hi there \(name)!".
There are lots of other things I enjoy, such as subscripts (see why here: https://docs.swift.org/swift-book/LanguageGuide/Subscripts.h...) and
Some things I would like to see improved is Xcode itself and also Swift's capabilities in the backend.
- rich enums - (https://appventure.me/2015/10/17/advanced-practical-enum-exa...) Much of my code is now encapsulated in the enum, where it logically belongs, instead of distributed throughout some class that consumes the enum.
- highly expressive pattern matching - (https://appventure.me/2015/08/20/swift-pattern-matching-in-d...) Swift's pattern matching is especially powerful with a switch statement. I frequently bundle multiple values into a tuple and switch across them, which allows me to flatten many if-else pyramids, and makes control flow more obvious up front.
- Optional - (https://developer.apple.com/documentation/swift/optional)
- Protocol extensions - (https://docs.swift.org/swift-book/LanguageGuide/Extensions.h...) Other languages use things like abstract classes or traits to implement this behavior, but I find protocols to be much more composable.
- The standard library - Swift's standard library is really well thought out. Many standard types have been built on top of highly reusable (and easy to reason about) types or interfaces that provide enormous utility when I adopt them in my custom types. Examples of this are: Codable (https://developer.apple.com/documentation/foundation/archive...), Equatable and Hashable (https://developer.apple.com/documentation/swift/adopting_com...)
There's more than that, and Swift is definitely not without it's shortcomings, but those features have fundamentally changed how I reason about code. I frequently find myself wishing I could reach for similar tools in other languages.
I know Apple was heavily involved in the Dylan language several years ago. Does anyone know if Swift was influenced by Dylan ?
A shared dyld cache would be a cache of metadata used for linking, but not sure if that is for link-time or runtime.
Of course, mixing with Objective-C may actually be the cause of issues or crashes? Who knows.
In fact, I would argue that this new trend of defensive programming in Swift will make software worse in the long run. We had a tradition of sending crash reports back to developers. If everyone now starts their methods with `guard let param = param else { return }`, software will silently fail on end user devices, and everything will look fine in Crashlytics/App Store Connect.
I'm not saying that this is what your apps are doing. But I know that Apple bragged about their record low in crash numbers at a time when I ran into different glitches across all of their apps every single day. It's a flawed metric.
However if you adopt different patterns with Swift you can exclude a lot of optionals, for example if you give every View a State enum with associated values you can avoid quite a lot of optionals:
class PersonView: UIView {
enum State {
case empty
case loading(personId: String)
case loaded(person: Person)
}
var state: State = .empty {
didSet {
switch state {
/* handle all different states */
}
}
}
}
Every time you switch state you need to give the right associated value and every time you are in this state this value is guaranteed to be there where before you would have an optional personId and an optional person.And it's applicable almost everywhere. I barely use optionals anymore unless I really can't replace them with an emum.
Also, take into account that in Objective-C you can send any message to a nil pointer, and the result will be nil or 0 (for scalar types), as defined by the language standard.
Swift is not defensive, Swift can be defensive, but it can not if you don't want to.
It's much more "offensive" than Objective-C, for example.
Just a "!" and it's the same as Null Pointer Exception == Fatal Error.
Foo was rewritten in Swift, and bar was super buggy. Why would you blame that on Swift?
Obviously, it's not Swift's fault for that, but even if people at Apple make rookie mistakes such as using force unwrap operator, it tells something about the language.
It's still often suggested by Xcode if you use an optional which has not been unwrapped in any way. "Click here to make error go away" adds an !.
Actually I was wondering with Swift had a lot of these things I don't see much in other languages. Optionals for example. Is it because it us focused on coding highly responsive UIs where different states might not yet be available?
But most new languages has some variant of this stuff: kotlin, scala, rust.
I program a lot in Julia which is a script language and it does not have allow you to use null freely either. May seem odd for a language that does not type check at compile time. But it actually makes sense.
C-family languages let you take off your whole leg with the slightest mistake.
This seems to contrast MS when they released .NET but shied from using it in their own systems (though I might be wrong)
The performance difference between the garbage collected runtime and its C predecessor was too much and initial attempts at the rewrite in C# with Longhorn were a major fail.
Then there was Longhorn, which failed more due to internal politics than due to technical issues, as Midori later has proven just to be killed by management, as decribed by Joe Duffy postmortem reports.
WinRT/UAP/UWP are built on top of improved COM, and .NET has a native personality for them, using AOT compiler for WinRT/UAP (MDIL) and UWP (.NET Native).
Most of the new UWP stuff is written in .NET Native, with C++ taking care of the UWP runtime infrastructure, and the DirectX composition engine (Visual Layer).
The majority of Windows UI team UWP and FluentUI demos are usually done in .NET Native.
The problem that we have is that we have so much infrastructure around C++ that it is expensive to switch. Even if you get faster compile times or theoretically less memory management bugs, you may lose the productivity in other ways if the rest of your tooling isn’t there.
With C# you definitely pay a small cost in memory consumption and a decent cost in binary size vs. C++ though, which still matters.
Silverlight was more of a platform than an "internal product" and it was based on .NET so it was only natural (it doesn't seem to have taken off though)
And yes .NET makes COM usable (well it is usable using MS libraries in C/C++, otherwise, forget about it :) )
On a side note, iOS 12 was great but seems riddled with memory leaks that eventually require a reboot every few days.
Are you sure you're not talking about storage space? I don't know what memory diagnostic app you're talking about.
Don’t forget to update your anti-virus too!
Sure it's not 'the memory diagnostic app' that's wrong?
How's that so? An app doesn't have to leak OS memory, just leak its own memory.
What I’ve noticed is that everything is faster. Until it suddenly isn’t. And then swiping between home screen pages takes approximately one full second and only shows one intermediate animation frame. In other words, it suddenly is unusable. Background app chewing up resources? No idea. I just know it didn’t do it in iOS 11.
Oh, and this was throughout the beta. It’s been decreasing as time goes on though.
Love, - guy who writes software and knows that all software is terrible
It really amazes me how people are always forming ideological groups - C vitamin is the best cure for everything. C vitamin is useless, just forget it. iOS has memory leaks. No, you are holding it wrong, iOS 12 is good.
And here at least we are a techy bunch, but outside the tech circle, how people treat software is just astonishing. Like the guy who made photographs sitting on the passenger seat, because his Tesla is a full autonomous car and it is perfectly capable of driving by itself.
>Are they using it in the top ten (mail, web, cal, clock, etc.)?
No.
>How much of Swift compared to ObjC code?
Much less.
It makes sense too. You don't rewrite perfectly good programs in a new language just for the fun of it.
The Holy Grail of software engineering
It also allowed us to get more people involved in development. People new to apple development found Objective-C to be a bigger barrier. Odd syntax and a bit old fashion.
Mind you I quite liked Objective-C. But it seems a bit pointless when you got Swift.