Programming in Dart: Functions & Closures

Jun 21 2022 · Dart 2.16, Flutter, DartPad

Part 2: Learn Anonymous Functions & Closures

16. Learn Generic Functions

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 15. Understand Generics Next episode: 17. Challenge: Write a Generic Function

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 16. Learn Generic Functions

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.

When we define a generic type, the convention is to use a capital single letter. For example, a capital T stands for type. Or a capital E means element. Mostly, you’ll be working with just T but if you have several types like a map, don’t be surprised by lots of different letters.

In the case of map, the types are indicated with a K and V, meaning Key and Value.

Just to be clear, the single letter just refers to the type and what I’ve just described is a convention so if you run into a single letter that doesn’t make any sense like Z, just know, all it represents is a type. That’s it.

When we create a generic method, we need to define our generic elements. These come after the functions name and define them in angled brackets. If you need more than one, separate them with a comma. Once defined, you can use them anywhere in the method signature where the type should go. Mind you, if you don’t define them, Dart will not understand the meaning of the type.

Okay, let’s write a generic function.

To get started, open up DartPad in your browser. We’re going to create a function that accepts three items and returns them in a list. Being we want this to work with with any type, we’re going to make it generic.

Let’s start by defining our generic function. First, we’re going to signify the return type. This function is just going to return a list so we write it as List.

List<T>

Remember, the T just signifies the type of list. It can represent a String, an int and so forth. Notice that we just a type without defining it. We have to do that now. Let’s give the function a name, followed by the types that we plan to use. We’re only using one type.

List<T> toList<T>

With that set, we’ll define the parameters. These are all using the T type.

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

}

Now we return the list with our items.

return [item1, item2, item3];

Okay, we’ve define our generic function. To break down our function signature again, remember, after the function name, we define our types. Everywhere else, we are using that type. When we call that function in our code, the T gets replaced with String or int or even objects which you’ll discover in the next course.

Let’s call it now. In our main function, let’s use our toList function. We will call it a score list passing in three scores.

var scoreList = toList<int>(58, 94, 78);

Here were created a list of ints by using our new function. We’ve explicitly marked the function taking integers. Even better, Dart can infer the typing so we can drop that explicit type.

var scoreList = toList(58, 94, 78);

Now let’s make a name list:

var nameList = toList('Brian', 'Larry', 'Curly');

And let’s print out the result.

print(scoreList);
print(nameList);

Now run the program. And look at that - we get two different lists. One is an integer list and the other is a string list all using the same functions. Now that is pretty awesome.