Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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
Oop Dart子类型和方法参数_Oop_Inheritance_Dart_Types_Subtyping - Fatal编程技术网

Oop Dart子类型和方法参数

Oop Dart子类型和方法参数,oop,inheritance,dart,types,subtyping,Oop,Inheritance,Dart,Types,Subtyping,我想了解飞镖式系统是如何工作的。 以下是一个片段: class Base {} class Derived extends Base {} void f(Derived x) {} void main() { Base x = Base(); f(x); } 至少在此基础上编译并给出以下运行时错误: Uncaught exception: TypeError: Instance of 'Base': type 'Base' is not a subtype of type 'De

我想了解飞镖式系统是如何工作的。 以下是一个片段:

class Base {}

class Derived extends Base {}

void f(Derived x) {}

void main() {
  Base x = Base();
  f(x);
}
至少在此基础上编译并给出以下运行时错误:

Uncaught exception:
TypeError: Instance of 'Base': type 'Base' is not a subtype of type 'Derived'
显然,接受有序集的方法不能只接受一般集

这不应该是可以在编译时捕获的吗?是否要设置一些编译标志,以便编译器能够检测到此类错误

UPD: 下面是一个更简单的例子:

void main() {
  num a = 2.5;
  int b = a;
}

关于这种行为,这里有一个悬而未决的问题:

如果您希望analyzer为此出错,可以在项目根目录中创建一个名为analysis_options.yaml的文件,该文件包含以下内容:

analyzer:
  strong-mode:
    implicit-casts: false

谢谢你!对于其他感兴趣的人,请检查(引用:“我现在强烈(双关语)认为默认情况下的隐式降级是一个错误,在将“null”引入语言的顺序上。”)和。我还有一个小问题,就是用
f(Base())
替换代码段中的main主体不会编译,即使这是一个微不足道的重构。我想这可能与…有关,对吗?