Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Learn Generic Functions

This video was last updated on Jun 21 2022

Understand the basics of writing generic functions. That is, writing a function that can be used by many different types.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

In the last episode, you were introduced to generics. You saw how the help you write code once that can be used for a lot of different types. Now, it’s time to actually write some generic code. One big thing to keep in mind - generic code must be, er, generic. Meaning, you must write code in a way that ensures that the type is able to be used in way you want to use it.

List<T>
List<T> toList<T>
List<T> toList<T>(T item1, T item2, T item3) {

}
return [item1, item2, item3];
var scoreList = toList<int>(58, 94, 78);
var scoreList = toList(58, 94, 78);
var nameList = toList('Brian', 'Larry', 'Curly');
print(scoreList);
print(nameList);

Reviews

Comments