Programming in Dart: Functions & Closures

Jun 21 2022 · Dart 2.16, Flutter, DartPad

Part 2: Learn Anonymous Functions & Closures

15. Understand Generics

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: 14. Create a Closure Next episode: 16. Learn Generic Functions

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: 15. Understand Generics

In this episode, you’re going to dip your toe into generics. Generics is a concept used in a lot of other languages as a way to simplify our code.

Earlier in this course, I introduced an important caveat when writing code. And that’s Don’t Repeat Yourself. That way, you have to change code in one area versus many areas.

But sometimes, you have no choice.

For the sake of simplicity, imagine you were creating a simple add function that adds two integers together. Easy as it gets. It receives two integers, adds them together, and returns the result.

Sounds good, but what if later, you wanted to use doubles as well. The code is the same, yet, the function doesn’t take doubles. The easy solution is to copy the original add function and change the types from ints to doubles. You’ve solved the problem. Or have you? You’ve violated the don’t repeat yourself rule. If you later want to change that method in order to log the operation to a database, you now have two places that you need to change.

But then later, you want the add function to work with strings and suddenly, you realize that your approach won’t scale. What’s worse with your solution is that code for all these different types are the same except for the types themselves.

Thankfully, Dart provides us generics avoid this issues. Generics allow us to define functions that work with any type instead of having three individual functions, we can just use one. We write the function indicating that it is generic and then when we use the function, we indicate the type we are going to use and the type it can return.

Believe it or not, you’ve been using generics throughout this entire course. Remember Lists? We’ve been using lists to hold strings, integers and various other types. That’s because Lists, like all other collections are generic. This means maps and sets are also generic. But lets just on lists for now.

When we signify the lists type, we use angled brackets that contains the type. Once you set the list’s type, you can never change it which ensures we only have a container that holds one type.

Lots of things can be generic. In the next episode, we’re going to be exploring generic functions but in the next course, you’ll also be working with generic classes.

Now generics can look a little strange, but you’ll get used to them. Also when you start developing, you’ll find yourself using a lot of generic objects like lists and maps as opposed to writing your own but in time, you’ll find places where functions and classes are best made generic.