繁体   English   中英

为什么 dart 没有抛出编译时错误类型的异常。 运行时?

[英]why dart didn't throw compile-time mistype exception instead. of run-time?

考虑这个示例代码:

void main() {

  List<int> array = <int>[];

  for (int i = 0; i < 5; i++) {
    array.add(i);
  }

  Iterable<int> newList = array.where( (value) =>  value % 2 == 0 );

  printFunction(array);
  printFunction(newList);
}

void printFunction(List<int> list) {
 print(list); 
}

此代码编译成功,但当然在运行时抛出异常,因为它无法运行printFunction(newList)的代码,除非在传递newList之前调用toList()

为什么编译器在这种情况下没有抛出异常???

这种检查可以通过 dart 分析器设置 (analysis_options.yaml) 启用,如下所示:

analyzer:
  strong-mode:
    implicit-casts: false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM