Rock Solid Python with Python Typing Transcripts
Chapter: Static vs. Dynamic Languages
Lecture: A Very Swift Motorcycle
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The last language that we're going to look at is Swift. Now, Swift, unlike .NET, does require you to have a Mac to run it, but you know what?
0:09
You already know what all this output looks like. It looks like this, plus or minus, true being uppercase or lowercase.
0:16
You don't really need to run this. Just let's look at it. So Swift, I said, I put that even farther to the right of C++ and farther to the right of C#.
0:26
That wasn't just because that's where it fit on the screen. That's because I think it is even more intense on the type information.
0:33
So over here, there's a couple things to notice again, class motorcycle. We say let model be a string. So colon string.
0:42
It turns out this is basically the same syntax as Python. I told you Swift is very Python inspired in lots of ways like notice no semicolons.
0:50
There's none of that business because turns out you don't need it. So why have them?
0:55
Self not this again like Python, but over here we specify the type again an enumeration a motorcycle type
1:03
I'm not gonna get tons of autocomplete. I get some actually because I installed the Swift
1:07
Intelligence plug-in for pyjarm, but whatever so we've got our engine and then our off-road off-road is different
1:15
Well, it's somewhat different. It's expressed differently than say that for example the C# one says it's not just a boolean, but it's a Bool, right?
1:26
If it were like this, it means it can be true or false. Right, but when you put the question mark there, it's a nullable
1:34
Boolean, right? And bool, is it there? I don't know. If it's there, it's a bool. But if it's not, then it's going to be nil in
1:42
this thing, right? In Python, that would be equivalent to none. So this is one of those more intense levels where you have to explicitly say
1:50
Whether or not it can can be not there right it can be set to none or nil Okay, so we go through here again
1:57
here we're passing in a strongly type one that can go into the that can go into this because Boolean also can always be a bull right and
2:07
Here though when we talk about Is this a boolean the off-road here? We say can't jump we have to say you know
2:15
explicitly cast this or explicitly pull the value out, we should probably do a test and say if it's no, we're going to return false.
2:22
Otherwise, we're going to explicitly pull this out, but we're kind of shortcutting it here. And after that, everything looks pretty similar.
2:30
You know, you've got the return type on the end. That takes a while to get used to, but I'm a fan of that these days.
2:37
We've got our description, which is like the string, the dunder stir. And then the use cases again, look really, really similar like this.
2:45
Boy, that almost looks like Python, doesn't it? Looks a lot like Python because Swift took a lot of its inspiration from Python itself.
2:54
And it says in order to run this, just so you can see we go over and we run Swift C against that in this particular folder.
3:02
There it is, we run Swift C. And you'll see that it'll take a while because it's compiling. There we go.
3:11
If we go back up here, there's a MC executable. So we'll just say slash MC. And there it is.
3:20
There's our native Swift application we've created that, you know, does exactly the same thing true is lowercase in this world, but pretty interesting.
3:31
If we switch back to our Python one, still more information over, you got to sort of
3:35
take it in a big bunch over here compared to this, but the type information is there. The symbols are a little bit cleaner than say the C# one as well.
3:48
Not so many semicolons and curly braces between these two, right? A little bit simpler, again, like Python.
3:53
All right, so those are all the different comparisons about what different static and
3:59
dynamic languages might look like when creating something similar in Python.