Algorithms + Data Structures = Programs - Episode 183: Swift with Doug Gregor
Episode Date: May 24, 2024In this episode, Conor and Bryce chat with Doug Gregor from Apple about the Swift programming language!Link to Episode 183 on WebsiteDiscuss this episode, leave a comment, or ask a question (on GitHub...)TwitterADSP: The PodcastConor HoekstraBryce Adelstein LelbachAbout the Guest:Douglas Gregor is is a Distinguished Engineer at Apple working on the Swift programming language, compiler, and related libraries and tools. He is code owner emeritus of the Clang compiler (part of the LLVM project), a former member of the ISO C++ committee, and a co-author on the second edition of C++ Templates: The Complete Guide. He holds a Ph.D. in computer science from Rensselaer Polytechnic Institute.Show NotesDate Recorded: 2024-04-29Date Released: 2024-05-24Swift Programming LanguageWWDC 2014 Swift AnnouncementSwift on LanguishIntro Song InfoMiss You by Sarah Jansen https://soundcloud.com/sarahjansenmusicCreative Commons — Attribution 3.0 Unported — CC BY 3.0Free Download / Stream: http://bit.ly/l-miss-youMusic promoted by Audio Library https://youtu.be/iYYxnasvfx8
Transcript
Discussion (0)
We have a new programming language.
The language is called Swift, and it totally rules. Welcome to ADSP, the podcast, episode 183, recorded on April 29th, 2024.
My name is Connor, and today with my co-host Bryce, we interview Doug Greger.
This is part four of a five-part interview, in today's interview we chat about the Swift programming
language. I spent I don't know if it was a significant amount of time but on two different
occasions one was when I was preparing for that talk and then another time I think I looked at it
while I was covering a meetup and I I remember actually in that talk, I kind of ranked the
languages that I looked at, Rust, Swift, C++, D and Haskell. And I think there was like three or
four different metrics I ranked them based on. And one of them was just like, I think it was like a
happy face emoji. It was just based on like, it wasn't like anything concrete, like a happy face emoji it was just based on like it wasn't like anything concrete like a combination
of ergonomics and how it makes you feel and swift won that like by a long shot and uh one of the
things that i i love about swift especially coming from c++ is to a like remarkable extent i could
just guess like what i thought the syntax would be after you kind of got a feel
for the language. And 90% of the time, maybe even more, you'd just be right. And it starts as such
a simple language. And I've heard Chris Lattner, either in podcasts or in talks that he's given,
talk about this idea of like infinite hackability, that it starts off feeling like Python,
like a very simple, you don't need a main function, you don't need all the overhead.
You know, Java is the classic example that gets picked on of the public static void main,
blah, blah, blah, and you got to include everything.
And I know that Java 21 or whatever version they're on now has drastically simplified
that hello world example.
But, you know.
Don't apologize for Java.
It took them until version 21 to simplify that.
We love, or at least I love all programming languages.
There's something to be learned from every language, regardless of how you feel about
it personally.
But anyways, the point being is that Swift is, it's remarkable in just how well it's
designed.
And the only remark that I had after that was that I tried to go and then build like
a mini game.
And I was, I mean, I usually do most of my stuff on a Linux box.
Now, most of these days I do WSL2 using Ubuntu.
And at the time, I think I tried to get some panel library for just sort of like, you know, JavaScript canvassing equivalent.
And I, at the time, and so this was back i want to say
like 2020 so this is now for four years ago roughly three and a half years ago i hit some
walls pretty quickly and most of the and you know i'm not a i'm not a swifter i'm not sure what you
call folks that program in swift a swifty i know that's already already taken. But most of the threads of the forums that I found basically
recommended me buying, you know, an Apple machine, a laptop or something. They said,
they gave you all the answers to what you wanted, but they said, if you really want a pleasant
experience, you should just go buy an Apple laptop, which obviously I wasn't about to go out
and do. But I know that a long-term initiative has always been to kind of make the experience,
the Swift experience on every platform just as good as the Mac experience.
So I guess my main question after once again, a little mini monologue is what's the, you
know, at currently Swift 5.10 and maybe Swift 6 in the future, what's the current state
of like comparison of cross-platform development? You know, are we at parity with, you know,
developing on a Mac machine or what's the story for someone trying to do it on Windows or someone
trying to do it on a Linux machine? Yeah, I think I would say we are rapidly approaching parity. So we are not exactly there. We have a very long history
on Apple platforms of servicing many millions of developers. So we've dealt with, you know,
we've made a smooth experience there. But things have been moving very quickly to improve the
experience everywhere else. And so if you looked in 2020, Linux support was there, but you could not have
used it on Windows. There was no way, right? Linux support is much more advanced. A whole lot more
of the package ecosystem works on Linux as well as Mac. Nowadays, there's editor support for VS
Code and Emacs and NeoVim and so on through the SourceKill SP project.
And Windows has come along really far, really fast in the last maybe year or two.
This has been due to the efforts of the browser company.
So this company makes a browser called Arc.
Okay, I think I've heard of it on one of the podcasts. There's a bunch of Opera, Brave,
and I think Arc is in that kind of
not Chrome, Firefox, or Safari.
Right, right.
It's one of the other browsers.
So the interesting thing about it is they're in Swift,
and they actually have a number of compiler developers
that have been doing the hard work
of making Swift work well on Windows.
Interesting. And with their efforts and others from the Swift team, developers that have been doing the hard work of making Swift work well on Windows.
And with their efforts and others from the Swift team, we've managed to bring up Windows,
get it up to language parity, start getting more libraries working. There are nice installers for Windows now that help you get Swift up and running there. And they're betting their product strategy
on the ability to use Swift to write all of their code in this browser and deploy it out to various platforms.
So, yeah, I think the experience isn't exactly the same, for sure, but it is rapidly getting better.
So many languages that get started by one shop or that that are
that are founded in one place and then become community projects and it takes a long time
to truly become multi-platform you know community owned and driven things. Rust was started by Mozilla. I'm sure other examples don't come to
mind, but I'm sure kind of- Google with Go, you know.
There we go. Yeah. And, you know, I think the length of the period in which the perception is this is X org's language is internally at Apple, but it was a language that
became the lingua franca on Apple's platforms, meaning that that heritage will be there for a
long time. But I do imagine that as one of the key Swift people, that it does cause some
difficulties in trying to grow the Swift audience, because there's probably a lot of people who have that assumption and bias that Swift is, is Apple's language and it's only for
the iOS development. And I wonder what, um, what do you, and what does the Swift team,
the Swift community do to try to, um, uh, break past that, uh, that perception.
Right. So I guess there's a couple things we do.
The one is to
acknowledge that perception head on.
So I gave a talk
back in November at a
conference and
I basically asked how many people here
know what Swift is about.
And there's a whole bunch of hands. That's fine.
What it is. I said, okay, how many of you, keep your hands up, think it's Apple's language for writing iOS apps.
And every, basically every hand stayed up like that. That is the perception, right? And I think
you're right that it comes from its enormous success in that ecosystem. So over just a small
number of years, maybe two years, two to three years out of the
gate from when it was introduced, it took over the entirety of iOS app development, right? All
the conferences, all the talks, the new things coming out were all Swift focused. And so I can
certainly understand. Why was that? Why was it so successful so quickly? So I think, obviously,
because it's such a wonderful language uh sorry i mean it's
true it's true i remember that there was a wwdc like announcement didn't the crowd like go go
crazy or i don't know if you want to say crazy but like i remember watching a talk where it was
announced like swift was announced and like the audience i don't know if it was a standing ovation
but like the reception was like amazing.
Like people were thrilled to hear that they were going to be getting, not to say anything
bad about Objective-C, but they were basically going to be getting a modern, you know, functional
flavored language of the 21st century to write their apps.
And the demo I think that was given was pretty amazing.
Like it was...
Objective-C has served us so well for 20 years. We absolutely
love it. But we had to ask ourselves the question, what would it be like if we had Objective C
without the baggage of C?
We did more than think about it. We have a new programming language.
The language is called Swift and it totally rules. Swift is fast, it is modern, it is
designed for safety and it enables a level of interactivity
and development that you've never seen on the platform.
When it comes to speed, Swift is great.
Compare Python, a popular scripting language, with Objective-C when it comes to something
like, let's say, complex object sort.
Objective-C is a lot faster, but Swift is faster still.
And take something like RC4 encryption, extremely computationally intensive.
Python gets utterly crushed in this particular benchmark.
But look at Swift.
Now, Swift is also modern with features like closures, generics, type inference, multiple return types, and namespaces.
You know how many people at home are going, what in the heck are these guys talking about?
Classic Apple, you know, they've rehearsed it.
It's a very, very nice introduction to this new thing that you're getting.
Yeah, I think there's some of that.
I think, you know, I do think this is a very nice language.
So, yeah, thank you.
But also, I think the community was ready for it.
I also think we made it really, really easy to get into.
This comes back to, I mentioned interoperability previously.
So on day one of the Swift experience, we had file-by-file interoperability with Objective-C.
So all of Apple's frameworks at the time were written in Objective-C, right?
There's like hundreds of these things that come with the SDK.
They were all Objective-C. You could use every single one of them very cleanly from within Swift. You could
write, you could take your giant Objective-C project, this app you'd been shipping for,
you know, years and write one single new source file in Swift and it would interoperate with your
Objective-C cleanly. So there was no risk involved in trying it. And you don't actually see this with
a whole lot of languages when they come out. Often you have to really buy in to a whole new stack,
a whole new package ecosystem to actually use the language in something.
We went this fine grained route so that many, many people were able to try it.
The great thing about that is we got a ton of feedback.
So we could iterate on the language, iterate on the tools,
refine things to make it the language that people wanted it to be over the next couple of years.
And so I think the combination of it being better,
it being a community that is used to change and used to adopting new technologies,
and also the fact that there
there was very little cost to trying it out meant that we were able to to move people pretty quickly
and i mean i i guess uh probably i guess a part of it too is being in an ecosystem with a a
centralized vendor that uh can can you know you didn't Swift didn't exist in a world where there were like
10 or 12 different organizations, all of whom needed to do that work to make all the frameworks
interoperable. It was like, that could be like a top-down decision from Apple that, hey, we need
to make this work. And there's one org that can sort of make the entire work
across the entire platform.
You try to do that in Linux, and you've got to talk to a whole bunch of different people,
all of whom have different goals and objectives.
And it's a lot harder, I think, to roll out the tools and the interop and the support
that you need to.
I think that's largely true. On the other hand,
we're still in a big organization. So, right. And Swift, the interoperability that Swift enjoys
with Objective-C is, or at least certainly initially, it was entirely based on the way
that we designed and built the compiler. So it is a compiler feature that, so Swift, the compiler, embeds the entire Clang compiler in it.
And the way we use a C or Objective-C or C++ library is we parse the headers.
And the compiler has a mapping from feature X in C and C++ and Objective-C over to the way that Swift would represent that same thing.
And then the code generator works along with Clang's code generator to make the API lineup.
It sounds a lot like what the Carbon project is trying to get out of their interoperable story.
I'm not sure how familiar you are with Carbon, but it's the...
I'm only familiar with their goals and so on.
I know they've made interoperability with C++
a very first-class feature in their world
and much of their strategy.
I think they have mentioned Swift's approach
because they certainly know about us.
Yeah, but it's interesting
because I think very few languages
make this a priority or succeed in this way.
And we've talked in this podcast before about how hard it is for new languages to get off the ground.
And I am a big believer in the power of inertia.
I like to say Bryce's rule of computing is that inertia is king.
And we've talked about this rule of thumb that a language has to be 10x better on some aspect to really break in.
But that's, I think, less true if you've got a great interoperability story.
And in fact, the interoperability can be the 10x improvement that, oh, wow, this is so easy for us to adopt and pick up and into migrates or something more modern.
And we can do it incrementally.
And that's how you get to the sort of rapid adoption that Swift's had. I'm not sure, Connor, if you have graphs or data to back this
up, but my understanding is that Swift is what, when Swift was, from when Swift was released
to like the first couple of years, it had very rapid growth trajectory relative to other new
languages or languages that reached the scales that it has.
Obviously, if you look at languages that go from like zero to a thousand users,
you know, that's not really a sample size that's sufficient to get the data.
But if you're talking about going from zeros to millions of users,
I don't think there's many languages that have done it as quickly as Swift has. I mean, I think that the language I would say is most similar
in sort of rollout strategy is probably TypeScript, because it did very much the same thing of there was a JavaScript ecosystem, which is massive. It provided interoperability and a much better programming experience for that ecosystem and enjoyed very fast adoption. I was just going to say, there's a website, we'll leave a link.
I think you can hyperlink exact sort of customizations of what you select.
And it's called Languish and it's based on GitHub statistics.
So, you know, it's all of these ranking things or take them with a huge grain of salt.
But if you actually look at TypeScript versus Swift from the period of like 2012, which is when this site goes back to, to 2016, 17, TypeScript is, they've got similar curves at first, but then something happened in 2014 and then Swift just takes off.
And according to these stats actually was higher ranked than TypeScript up until 2017.
And then 2017, I think, you know, TypeScript is slowly taking over the JavaScript world.
But yeah, like there's this period of two, two and a half years where Swift is, it's
growing at this incredible rate, even faster than TypeScript at the time.
Yeah.
So Swift was introduced in 2014.
So.
Well, I have no idea what the data prior to 2014 is.
So, it says that in 2013 Q4, in 2013 Q3, it does have some numbers.
So, I don't know what that's from.
So, here's a possible theory.
You said it's from GitHub data, right?
Mm-hmm.
Yeah.
So, it's possible that the Swift projects, GitHub, Git history is now on
GitHub and that it's looking at stuff from before it was publicly released. So that's probably.
The Swift compiler projects, Git history goes all the way back to the dawn of time,
which obviously predates when we launched it. So that that's probably where it comes from,
but you would have a small number of users at that point.
So one other thing that I think of when I think of Swift is I think of it as a teaching or an education language.
And even from the launch of Swift,
there has been a big focus on education,
not just of programmers,
but of people who are learning to program for the first time.
And not just of, like, career professional programmers who already, you know, know another
language, but, you know, as a first language for people.
Like, when I think of first language to recommend to people, Swift's often one that comes to
mind.
Was that a focus from the start?
You know, how did that come to be?
So, I think it follows from one of our, know, how did that, how did that come to be? So I think it follows from one
of our like philosophical points. So we believe very deeply in this notion of progressive disclosure,
the idea that when you're starting out, things should be simple and straightforward. And as you
grow, you start to need more capabilities. Those capabilities should
be there and you can go learn about them, incorporate them. But there's two sort of
buts. First of all, they shouldn't come to you way too early. So you shouldn't be forced to
deal with a super advanced feature just because you step slightly outside of the bounds.
And the other part is it shouldn't invalidate
anything that you've learned so far. And so we do care a lot about the hello world experience,
right? Like you print hello world and it works. And so that's it, like just print and then
are in quotes. Hello world is fine. And we want it to be fairly simple to then grow into doing
simple data structures and
building, you know, your own types with structs and enums and so on. And we do like that it can
be used as a learning language. I think it is valuable that people can learn a language and
it can grow with them, right? So when they're ready, they can say, you know what? I want to
learn about generic programming. You know, Swift has a generic system. say, you know what? I want to learn about generic programming.
You know, Swift has a generic system.
And, you know, the fun part about that is you can go and learn about generics and you write your identity functions and you learn how to write a protocol, which is basically Swift speak for a concept.
And you write some generic algorithms. And then you realize that you've been using generics in your entire Swift existence.
Because array is just a generic type defined in the library.
And you've been writing like filter and map operations on arrays as part of, you know,
just writing code.
And those are simply generic algorithms that are expressed in the library on like the collection protocol. And so it was always there, right? Like this underlying more complex model was always there, butics. You have to have that down solid.
Because there's nothing that kills progressive disclosure worse than something like a template and backtrace.
That just throws you into all the complexity.
But if you can keep it there but not required,
you can have a language that is both you know complex to to
model all the complicated scenarios that we need when we're doing like you know full full-on
software engineering but still keep it simple and approachable enough well and there's also the the
the fact that it um is so great uh for programming uh like GUIs and apps
and integrates so nicely with the Apple frameworks.
I think makes it a really good teaching language,
especially for like kids.
And there's the, is it called Swift Playgrounds
or something like that?
What's the name of the thing, the app on?
Right, yeah.
So the Swift Playgrounds app lets you learn Swift
on an iPad.
So, you know, it's, it's a full Swift compiler,
uh, under the hood, but there's, you know, guided tutorials to help learn aspects of the language
in a fun and engaging way. And then you can go off and code whatever you want.
And I, I, I've checked it out and it's like, I think it's like, like, especially for like, uh,
somebody under 18, like, I think it's by far the best way to introduce programming to somebody.
Like, I've tried to look at, you know, for family members, like, you know, how could, you know, a 13 or 14-year-old kid or even younger, like, learn, like, programming language.
And, like, for the things that come to mind to me are, like, Python or Swift.
And for Python, like, I can go, like, look for things. Like, yeah, there's, like, some things out there. There's, like python or swift and for python like i can go like
look for things like yeah there's like some things out there there's like a bunch of different like
courses but for like swift there's just this like this thing that's like like a a native like first
class thing that's from the the people who make the language that you can just get on your ipad
that's great um and it doesn't require a lot of setup like Like you can do it on an iPad, which is how like young people, you know, access the world these days.
And it's just like it's so next level in terms of programming education.
And I wonder whether there will be a long tail effect there of, you know, down the road,
there will be a lot more people with proficiency in Swift because they will have learned it at a younger age because it's such a good experience.
It's possible. And I would expect that people also come through the, I want to build an app, what do I do realm where we have, this is an Apple technology, but SwiftUI is a declarative UI framework. It's an interesting case because if you look at SwiftUI,
the UI is expressed in Swift code. It's quite elegant. It's very simple. You've got text fields
and buttons and so on. And you don't even necessarily have to be a programmer to pick
up enough to start working with it. I know designers have used it to be able to do essentially real code mock-ups.
And it's one of those cases where I think we have succeeded at progressive disclosure
because you can learn it as a first thing to do some UIs in this sort of fun and engaging way.
Now, what's behind SwiftUI is a pretty amazing generic library. It uses all the features of the Swift generic system,
but you're not confronted with it
when you're working at the UI layer.
It's there if you need it.
So yeah, it's been fun, right?
We're trying to build a language for everyone.
We hope that it can be your first language
and we hope that it can grow with you
as you do more sort of big and complicated things.
I do feel like we've sort of done you a disservice in that we didn't ask you about Swift generics
30 minutes ago.
And I worry that we may not be able to do it justice in the second hour of our recording.
Whoa, what do you mean second hour?
Third hour of our recording.
Sorry.
Counting is Bryce's weakness in this podcast, clearly.
I think maybe, I mean, do we want to dig into it now, Connor?
Or should we maybe just plan to have Doug back for...
We can definitely have you back, Doug.
But I mean, I think we should definitely, I mean, we can't mention you.
There's no way we can leave the listener. I mean, they've been, we did leave the listener wanting an explanation for this, at least for a couple episodes, because Lord knows how many episodes this is going to get cut into. The record is five, but this could potentially become six episodes. But I'm, my guess is that I'm going to cut these maybe a little bit longer so that we don't, I think our listeners are going to be upset if we take this whole podcast recording and then I dice it over a month and a half.
I think they'll just – they'll end every 30-minute segment being like, what are we doing here? just been like itching to to record with connor about like these these various unrelated to this
discussion algorithm uh inspirations and insights i've had recently so almost certainly there'll be
more there'll be more episodes cut in between these but yeah let's give doug a chance to
tell us a little bit about the swift genetics i think i think it'll be it'll be back to back to
back doug doug doug doug doug for it'll either be over a month or over six weeks.
But definitely tell us a little bit about the generics in Swift.
But also, you mentioned way back, it was probably an episode or two ago for the listener.
You mentioned that, you know, half of its lifetime has been working up to Swift 6. Be sure to check these show notes either in your podcast app or at ADSPthepodcast.com
for links to anything we mentioned in today's episode, as well as a link to a GitHub discussion
where you can leave thoughts, comments, and questions.
Thanks for listening.
We hope you enjoyed and have a great day.
Low quality, high quantity.
That is the tagline of our podcast.
That's not the tagline.
Our tagline is chaos with sprinkles of information.