hey sean! i really really appreciate the chill and slower talking.. i know everyone talks super fast on YT , but on programming videos and such giving time to think is muuch needed. great video! very well explained 👏
Well explained the usage of generic type! However, when I watch this video, I was expecting to hear about the difference of function with generics (similar to "template" in C++) vs. function with protocol-typed parameters. Basically, Swift allows both to achieve polymorphism. The following code would have exactly same outcome with driveHome() and genericDriveHome(): protocol Drivable { func drive() } struct Motorcycle: Drivable { func drive() { print("I'm a motorcycle. I'm going home.") } } struct Car: Drivable { func drive() { print("I'm a car. I'm going home.") } } func driveHome(vehicle: Drivable) { vehicle.drive() } func genericDriveHome(vehicle: T) { vehicle.drive() } let m = Motorcycle() let c = Car() driveHome(m) genericDriveHome(m) driveHome( c) genericDriveHome( c) In fact, if I use the same func name: func driveHome(vehicle: T){ vehicle.drive() } and call driveHome(m), the protocol typed function is favored. . . . I found the answer on StackOverflow, if you have the same question as me: stackoverflow.com/questions/38446487/what-is-the-in-practice-difference-between-generic-and-protocol-typed-function-p/41490551#41490551
Sean, what’s your recommendation for someone to learn Swift coming from a C++ background 25 years ago, then Java 17 years ago. I’ve done virtually no coding since 2006, but want to get back into it for Vision Pro. (Shoot me any questions you have if needing to lock down further background information before making a recommendation.) Thanks much! I really want to do some awesome Vision Pro apps.
Want to go deeper with Swift and iOS Dev? Check out my iOS Dev courses - seanallen.teachable.com
hey sean! i really really appreciate the chill and slower talking.. i know everyone talks super fast on YT , but on programming videos and such giving time to think is muuch needed. great video! very well explained 👏
Thanks. I usually get comments about speaking too fast 😂
Finally, I understand Generics! Thanks, Sean.
Happy to hear it!
Well explained the usage of generic type! However, when I watch this video, I was expecting to hear about the difference of function with generics (similar to "template" in C++) vs. function with protocol-typed parameters. Basically, Swift allows both to achieve polymorphism. The following code would have exactly same outcome with driveHome() and genericDriveHome():
protocol Drivable {
func drive()
}
struct Motorcycle: Drivable {
func drive() {
print("I'm a motorcycle. I'm going home.")
}
}
struct Car: Drivable {
func drive() {
print("I'm a car. I'm going home.")
}
}
func driveHome(vehicle: Drivable) {
vehicle.drive()
}
func genericDriveHome(vehicle: T) {
vehicle.drive()
}
let m = Motorcycle()
let c = Car()
driveHome(m)
genericDriveHome(m)
driveHome( c)
genericDriveHome( c)
In fact, if I use the same func name:
func driveHome(vehicle: T){
vehicle.drive()
}
and call driveHome(m), the protocol typed function is favored.
.
.
.
I found the answer on StackOverflow, if you have the same question as me: stackoverflow.com/questions/38446487/what-is-the-in-practice-difference-between-generic-and-protocol-typed-function-p/41490551#41490551
Sean allen you are the best, explain everything very nice and simple so everyone can understand it easily 😊👍
Thanks for the kind words, Saeed.
1:50 So when to use generics as opposed to just "some Drivable"?
Good video :) you can use the max(_:_:) function which takes two Comparable types.
print(max(valueOne, valueTwo), "is the highest value")
I knew there was a better way.
Thank you for the video!
You explain complex things in simple terms!
Glad you enjoy them, Ruslan!
Great explanation and examples
Thanks Jason!
Thank you for the video!
My pleasure!
Can you do a video on protocols?
It's on my very long video idea list. So many video ideas, so little time :)
Can you do a video for "Opaque Types"?
I'll add it to the idea list. However that list is VERY long :)
Which theme are you using in xcode
It's called Midnight
How do you choose the protocol? like Decodable, comparable?
One thing I don't understand, since you provide protocol for Generic type, why not just use protocol directly.
Sean, what’s your recommendation for someone to learn Swift coming from a C++ background 25 years ago, then Java 17 years ago. I’ve done virtually no coding since 2006, but want to get back into it for Vision Pro. (Shoot me any questions you have if needing to lock down further background information before making a recommendation.) Thanks much! I really want to do some awesome Vision Pro apps.
❤❤
Glad you liked it!