Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function Dart-将函数声明为变量_Function_Variables_Dart_Function Declaration - Fatal编程技术网

Function Dart-将函数声明为变量

Function Dart-将函数声明为变量,function,variables,dart,function-declaration,Function,Variables,Dart,Function Declaration,在Dart中——和许多其他语言一样——声明函数的方法不止一种。问题是,有没有什么不同,比如“我应该在什么时候使用哪一种” foo或bar的声明是否有任何区别,除了可以覆盖bar之外?函数可以由 函数声明 定量方法 getter声明 setter声明 构造函数声明 函数文字 在函数文本(又称匿名函数)和其他声明之间有2个区别 它没有名字-匿名 我们不能声明返回类型(只能通过类型推断获得) 如果您希望保持类型安全,则必须编写较长的声明 考虑这个例子: String foo(int i, {boo

在Dart中——和许多其他语言一样——声明函数的方法不止一种。问题是,有没有什么不同,比如“我应该在什么时候使用哪一种”


foo
bar
的声明是否有任何区别,除了可以覆盖
bar
之外?

函数可以由

  • 函数声明
  • 定量方法
  • getter声明
  • setter声明
  • 构造函数声明
  • 函数文字
在函数文本(又称匿名函数)和其他声明之间有2个区别

  • 它没有名字-匿名
  • 我们不能声明返回类型(只能通过类型推断获得)
  • 如果您希望保持类型安全,则必须编写较长的声明

    考虑这个例子:

    String foo(int i, {bool b}) => '$b $i'; // return type declared
    
    final bar = (int i, {bool b}) => '$b $i'; // return type could not be infered
    
    final String Function(int i, {bool b}) bar = (i, {b}) => '$b $i'; // return type infered
    
    在我看来

  • bar
    的可读性不如
    foo
    声明
  • 让函数文本执行其匿名作业=)

  • PS如果我遗漏了什么-请编辑我的答案或在评论中联系我

    • 函数声明
    • 定量方法
    • getter声明
    • setter声明
    • 构造函数声明
    • 函数文字
    在函数文本(又称匿名函数)和其他声明之间有2个区别

  • 它没有名字-匿名
  • 我们不能声明返回类型(只能通过类型推断获得)
  • 如果您希望保持类型安全,则必须编写较长的声明

    考虑这个例子:

    String foo(int i, {bool b}) => '$b $i'; // return type declared
    
    final bar = (int i, {bool b}) => '$b $i'; // return type could not be infered
    
    final String Function(int i, {bool b}) bar = (i, {b}) => '$b $i'; // return type infered
    
    在我看来

  • bar
    的可读性不如
    foo
    声明
  • 让函数文本执行其匿名作业=)

  • PS如果我遗漏了什么-请编辑我的答案或在评论中联系我

    范围也有所不同。您的
    bar
    函数位于
    main
    方法范围内,因此可以访问
    main
    中定义的变量(例如,如果您在
    bar
    变量之前定义变量)…范围也存在差异。
    bar
    函数位于
    main
    方法范围内,因此可以访问
    main
    中定义的变量(例如,如果在
    bar
    变量之前定义变量)。。