The semantics of typedef in dart

Muhammad Waris
Embrace-It
Published in
3 min readNov 12, 2020

Have you ever come across a situation while coding when you know what kind of return type a function will have but you haven’t yet written that function or a type that is going to be returned?

Well, In that case, dart offers typedefs for its developers.

let’s first look at the definition of dart’s typedef:

Definition:

A typedef, or function-type alias, gives a function type a name that you can use when declaring fields and return types. A typedef retains type information when a function type is assigned to a variable.

lets, dig deep into the definition and try to understand what it actually means.

Basically, typedef is a function alias or an assumed identity that you can use to specify the type of function or a variable while writing the code. it tells the compiler to expect the same typedef like return value while executing this piece of code.

Syntax & Example:

So, the typedef is a way of describing the type of a variable or a function return type without having the function in a fully implemented manner.

Now, Let’s look at the syntax of how we can define the typedefs in the dart.

Syntax 1 | Bad:

typedef return_type FunctionName(parameters);

E.g

typedef bool SendMessage(String message);

Syntax 2 | Good:

typedef FunctionName = return_type Function(parameters);

E.g

typedef SendMessage =  bool Function(int num);

So, now we have learned about the syntax and what typedef actually is. Now it's time to have a look at an example and use of typedef.

Naming convention:

typedefs should capitalize the first letter of each word (including the first word), and use no separators.

typedef Send = void Function(String message);void SendTextMessage(String msg) {
//Some stuff related to SMS logic
print(msg);
}
void SendEmail(String msg) {
//Some stuff realted to email client logic
print(msg);
}
void main() {
// assign your typedef to any function matching the signatures.
Send sendMessage = SendTextMessage;
sendMessage("Hello! this is a text message");
// you can also change the return type and reassign on the fly.
sendMessage = SendEmail;
sendMessage("Hello! this is an email");
}

In the above code, we have two functions SendTextMessage() & SendEmail() both have the same signatures so instead of calling the functions directly you can use the typedef and define the signatures of both functions that you can assign later to this typedef as shown in the main(). Also, you can reassign your typedef function with a diff function on the fly.

Signatures of your functions() and defined typedef should exactly match with each other

Uses:

  1. Typedefs are used for writing clean code. The code becomes easy to understand and self-explanatory.
  2. You can use the same typedef function name as a return type for all the similar kinds of functions with the same signatures.
  3. In Dart, typedefs are extensively used in Callbacks.
  4. Since everything is an Object in dart & Object type comparison becomes really important in some situations. Typedefs always retain their type information. So it's super helpful to use typedefs in such situations.

--

--