What are function typedefs / function-type aliases in Dart?

TypedefDart

Typedef Problem Overview


I have read the description, and I understand that it is a function-type alias.

But how do I use it? Why declaring fields with a function-type? When do I use it? What problem does it solve?

I think I need one or two real code examples.

Typedef Solutions


Solution 1 - Typedef

A common usage pattern of typedef in Dart is defining a callback interface. For example:

typedef void LoggerOutputFunction(String msg);

class Logger {
  LoggerOutputFunction out;
  Logger() {
    out = print;
  }
  void log(String msg) {
    out(msg);
  }
}

void timestampLoggerOutputFunction(String msg) {
  String timeStamp = new Date.now().toString();
  print('${timeStamp}: $msg');
}

void main() {
  Logger l = new Logger();
  l.log('Hello World');
  l.out = timestampLoggerOutputFunction;
  l.log('Hello World');
}

Running the above sample yields the following output:

> Hello World
> 2012-09-22 10:19:15.139: Hello World

The typedef line says that LoggerOutputFunction takes a String parameter and returns void.

timestampLoggerOutputFunction matches that definition and thus can be assigned to the out field.

Let me know if you need another example.

Solution 2 - Typedef

Dart 1.24 introduces a new typedef syntax to also support generic functions. The previous syntax is still supported.

typedef F = List<T> Function<T>(T);

For more details see https://github.com/dart-lang/sdk/blob/master/docs/language/informal/generic-function-type-alias.md

Function types can also be specified inline

void foo<T, S>(T Function(int, S) aFunction) {...}

See also https://www.dartlang.org/guides/language/language-tour#typedefs

Solution 3 - Typedef

typedef LoggerOutputFunction = void Function(String msg);

this looks much more clear than previous version

Solution 4 - Typedef

Just slightly modified answer, according to the latest typedef syntax, The example could be updated to:

typedef LoggerOutputFunction = void Function(String msg);

class Logger {
  LoggerOutputFunction out;
  Logger() {
    out = print;
  }
  void log(String msg) {
    out(msg);
  }
}

void timestampLoggerOutputFunction(String msg) {
  String timeStamp = new Date.now().toString();
  print('${timeStamp}: $msg');
}

void main() {
  Logger l = new Logger();
  l.log('Hello World');
  l.out = timestampLoggerOutputFunction;
  l.log('Hello World');
}

Solution 5 - Typedef

Typedef in Dart is used to create a user-defined function (alias) for other application functions,

Syntax: typedef function_name (parameters);

With the help of a typedef, we can also assign a variable to a function.

Syntax:typedef variable_name = function_name;

After assigning the variable, if we have to invoke it then we go as:

Syntax: variable_name(parameters);

Example:

// Defining alias name
typedef MainFunction(int a, int b);

functionOne(int a, int b) {
  print("This is FunctionOne");
  print("$a and $b are lucky numbers !!");
}

functionTwo(int a, int b) {
  print("This is FunctionTwo");
  print("$a + $b is equal to ${a + b}.");
}

// Main Function
void main() {
  // use alias
  MainFunction number = functionOne;

  number(1, 2);

  number = functionTwo;
 // Calling number
  number(3, 4);
}

Output:

This is FunctionOne
1 and 2 are lucky numbers !!
This is FunctionTwo
3 + 4 is equal to 7

Solution 6 - Typedef

Since dart version 2.13 you can use typedef not only with functions but with every object you want.

Eg this code is now perfectly valid:

typedef IntList = List<int>;
IntList il = [1, 2, 3];

For more details see updated info: https://dart.dev/guides/language/language-tour#typedefs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeroView Question on Stackoverflow
Solution 1 - TypedefCutchView Answer on Stackoverflow
Solution 2 - TypedefGünter ZöchbauerView Answer on Stackoverflow
Solution 3 - TypedefWesley Wookjin JungView Answer on Stackoverflow
Solution 4 - TypedefTed ZhangView Answer on Stackoverflow
Solution 5 - TypedefJitesh MohiteView Answer on Stackoverflow
Solution 6 - TypedefIlia KurtovView Answer on Stackoverflow