Programming in Dart: Functions & Closures

Jun 21 2022 · Dart 2.16, Flutter, DartPad

Part 1: Meet the Function

07. Understand Typedef

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: 06. Store a Function Next episode: 08. Use Arrow Notation

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: 07. Understand Typedef

In the last episode, we passed a function to a function and in doing so, we created a function signature that was a little complex and unwieldy. Dart allows to use an alias when referring to functions and other types. In such a way, it allows to create a shorthand alias so instead of using a clunky method signature, you use the alias instead. You can typedef used throughout the Flutter codebase.

You simply use the keyword typedef, followed by the alias name. After which, you provide an equals sign and type that you want to alias.

Doing this, you can create different names any type but remember, with great power comes great responsibility. Use your alias sparingly. If you start using different names for common types, your liable to confuse anyone who works on your project so keep the typedef in check.

To get started, open up DartPad in a browser. We’re going to add our function to multiply numbers.

int multiply(int a, int b) {
    return a * b;
}

Next, we’ll create a function to process a list of values.

int processScores(List<int> scores, int Function(int, int) processor) {
    var total = 0;
    for (var score in scores) {
        total += processor(score, 2);
    }
    return total;
}

Now we will call it.

void main() {
  var scores = [54, 75, 32];
  print(processScores(scores, multiply));
}

Okay we have our unwieldy function. Now, let’s give it a name. First, we’ll start with a keyword.

typedef

Next we’ll give it a name. In this case, we’ll call it ScoreModifier.

typedef ScoreModifier

Notice that I capitalized it. In general, we label our types in uppercase. Now there are some types that are lowercase such as int or double but those are the exceptions intended to ease the transition for developers coming from other languages like Javascript or C++. In general, when you define a type, it should always be uppercase unless there’s a good reason why it shouldn’t.

Okay, now we assign our function signature to the ScoreModifier.

typedef ScoreModifier = int Function(int, int);

Now, whenever we use the ScoreModifier, it is representing our function signature. Let’s replace our function header with our alias.

int processScores(List<int> scores, ScoreModifier processor) {

Our function signature is much easier to read. On an aside, this shows the importance of naming types and variables. Now down the road, you may forget what a ScoreModifier is, but you can just click on the type, and look at the type in the editor’s built in documentation.