Generic functions are not constrained to predefined types. In this video,explore how to implement a generic function and how it can be used with a variety of types.
- [Instructor] Implementing a new function…for every type leads to a lot of redundant code.…To avoid code duplication we can combine…all the different functions into…a single generic function.…Let's see how.…I open up our project in the exercise files,…chapter three, 0302, start.…To create a generic function,…we need to provide at least one placeholder type.…The placeholder must appear right…after the function name between angle brackets.…
Note that we could've used any other identifier.…Capital T is just a popular way…to defer to generic placeholders.…From now on we can refer to this placeholder type…in the argument list or anywhere in the function's body.…So, let's add the argument list.…It has a left hand side argument…and I'm going to use a placeholder type…and same for the right hand side argument.…The function returns as usual,…a Bool value.…
Let's implement it.…I'm going to return the result…of comparing the two arguments.…As you can see,…we get an error.…Binary operator equals…can not be applied to two T operants.…
- Comparing object-oriented programming with protocol-oriented programming
- Methods and class-bound protocols
- Adopting a protocol
- Declaring asynchronous behavior
- Preparing and implementing fallback logic
- Implementing an app using protocol-oriented programming
Skill Level Intermediate
1. Protocol-Oriented Programming
4. Implement the Weather App Using POP
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.