Dart Basic- Typedef

Syntax and Usage of Typedefs in Dart


In Dart, a typedef is a type alias that allows you to define a function signature. It can be used to create custom types that represent functions, making your code more concise and easier to read.

Here’s an example:

typedef String GreetingFunction(String name);
String englishGreeting(String name) => 'Hello, $name!';
String spanishGreeting(String name) => '¡Hola, $name!';
String swahiliGreeting(String name) => 'Habari, $name!';

void main() {
  GreetingFunction greet = englishGreeting;
  print(greet('John')); // Output: Hello, John!

  greet = spanishGreeting;
  print(greet('Juan')); // Output: ¡Hola, Juan!

  greet = swahiliGreeting;
  print(greet('Wanjiru')); // Output: Habari, Wanjiru!
}

Output:

Hello, John!
¡Hola, Juan!
Habari, Wanjiru!


In this example, we define a GreetingFunction typedef that represents a function that takes a String argument and returns a String. We then define three functions that match this signature for three different languages.

In the main function, we create a GreetingFunction variable called greet and set it to englishGreeting. We then call greet(‘John’), which prints Hello, John!.

We then set greet to spanishGreeting and call it with the name Juan, printing ¡Hola, Juan!. Finally, we set greet to swahiliGreeting and call it with the name Wanjiru, printing Habari, Wanjiru!.

By using typedefs to represent our GreetingFunction, we can easily swap out different functions that match the same signature. This can be particularly useful in larger codebases where you may have many functions that take the same arguments and return the same type. By using typedefs, you can create custom types that are more expressive and easier to read than using raw function signatures.

Uses of Typedef in Dart


Typedef in Dart is used to create a user-defined identity(alias) for a function, and we can use that identity in place of the function in the program code. 

Syntax:

typedef function_name ( parameters );

Example:

// Dart program to show the usage of typedef
 
// Defining alias name
typedef EnableGeek(int a, int b);
 
// Defining Geek1 function
Geek1(int a, int b) {
print("This is Geek1");
print("$a and $b are lucky geek numbers !!");
}
 
// Defining Geek2 function
Geek2(int a, int b) {
print("This is Geek2");
print("$a + $b is equal to ${a + b}.");
}
 
// Main Function
void main()
{
// Using alias name to define
// number with Geek1 function
  EnabkeGeek number = Geek1;
// Calling number
number(1,2);
 
// Redefining number
// with Geek2 function
number = Geek2;
// Calling number
number(3,4);
}
 

Output:

This is Geek1
1 and 2 are lucky geek numbers !!
This is Geek2
3 + 4 is equal to 7.

Benefits of Using Typedefs in Dart


In Dart, a typedef is a type alias that allows you to define a function signature, and then use that signature to specify the type of function parameters, instance variables, or return values.

A typedef is essentially a way to create a custom type that is based on an existing type, but with a different name. It is a shorthand way to define the type of a function.

Here’s the basic syntax of a typedef:

typedef FunctionName = FunctionType;

FunctionName is the name of the typedef. FunctionType is the signature of the function that you want to alias.

Here’s an example:

typedef MathOperation = int Function(int, int);
int add(int x, int y) {
  return x + y;
}

int multiply(int x, int y) {
  return x * y;
}

void main() {
  MathOperation operation = add;
  print(operation(3, 4)); // Output: 7

  operation = multiply;
  print(operation(3, 4)); // Output: 12
}

Output:

7
12


In this example, we defined a MathOperation typedef that specifies the signature of a function that takes two integer parameters and returns an integer. We then defined two functions add and multiply that match the MathOperation signature. Finally, we create a variable operation of type MathOperation and assign it to the add function. We then call operation with arguments 3 and 4 and print the result.

One of the main benefits of using typedefs is that they can make code more readable and maintainable. By creating custom types that represent function signatures, you can make your code more self-documenting and easier to understand. Typedefs can also make your code more modular by allowing you to swap out functions with different implementations without changing the function signatures throughout your codebase.

Another benefit of using typedefs is that they can make your code more flexible. By defining custom types for function signatures, you can create functions that take other functions as arguments or return functions as values. This can be particularly useful in functional programming paradigms, where functions are treated as first-class citizens.

So, typedefs are a powerful feature in Dart that can help you write more expressive and modular code. By creating custom types that represent function signatures, you can make your code more readable, maintainable, and flexible.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.