Dart中的typedef是什么?

Ger*_*ero 49 typedef dart

我已经阅读了描述,我知道它是一个函数类型的别名.

但是我该如何使用它?为什么用函数类型声明字段?我什么时候使用它?它解决了什么问题?

我想我需要一两个真正的代码示例.

Cut*_*tch 67

Dart中typedef的常见用法模式是定义回调接口.例如:

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');
}
Run Code Online (Sandbox Code Playgroud)

运行上面的示例会产生以下输出:

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

typedef行表示LoggerOutputFunction接受String参数并返回void.

timestampLoggerOutputFunction匹配该定义,因此可以分配给out字段.

如果您需要另一个例子,请告诉我.

  • 注意; 这个答案使用了传统的typedef符号。首选的是`typedef LoggerOutputFunction = void Function(String msg);`(来自https://www.dartlang.org/guides/language/effective-dart/design#dont-use-the-legacy-typedef-syntax) (7认同)
  • 不要忘记,在 Dart 中一切都是对象,甚至是函数(方法)。这意味着您可以将函数分配给变量。这可能会让 Java 用户感到惊讶。 (2认同)

小智 16

typedef LoggerOutputFunction = void Function(String msg);
Run Code Online (Sandbox Code Playgroud)

这看起来比以前的版本清晰得多


Gün*_*uer 15

Dart 1.24引入了一种新的typedef语法,以支持通用函数.仍支持以前的语法.

typedef F = List<T> Function<T>(T);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅https://github.com/dart-lang/sdk/blob/master/docs/language/informal/generic-function-type-alias.md

函数类型也可以内联指定

void foo<T, S>(T Function(int, S) aFunction) {...}
Run Code Online (Sandbox Code Playgroud)

另请参见https://www.dartlang.org/guides/language/language-tour#typedefs


jit*_*555 7

Typedef在Dart中用于为其他应用程序函数创建用户定义函数(别名),

Syntax: typedef function_name (parameters);
Run Code Online (Sandbox Code Playgroud)

在 typedef 的帮助下,我们还可以将变量分配给函数。

Syntax:typedef variable_name = function_name;
Run Code Online (Sandbox Code Playgroud)

分配变量后,如果我们必须调用它,那么我们将执行以下操作:

Syntax: variable_name(parameters);
Run Code Online (Sandbox Code Playgroud)

例子:

// 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);
}
Run Code Online (Sandbox Code Playgroud)

输出:

This is FunctionOne
1 and 2 are lucky numbers !!
This is FunctionTwo
3 + 4 is equal to 7
Run Code Online (Sandbox Code Playgroud)


小智 6

根据最新的 typedef 语法,稍微修改了答案,该示例可以更新为:

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');
}
Run Code Online (Sandbox Code Playgroud)