为什么抽象类实例化不是 dart 中的运行时错误? [英] why abstract class instantiation isn't runtime error in dart?

查看:19
本文介绍了为什么抽象类实例化不是 dart 中的运行时错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多语言中,如果您尝试实例化抽象类,则会出现编译时错误.但是,在 Dart 中,您会在编译时收到警告和运行时异常 AbstractClassInstantiationError.

In many languages if you try to instantiate abstract class you get compile time error. In Dart however, you get warning while compiling and a runtime exception AbstractClassInstantiationError.

这是为什么?有人可以提供一个例子,编译这样的代码是合理的吗?

Why is that? Can someone provide an example, where it's reasonable to compile such code?

推荐答案

Dart 试图让您在开发程序的同时运行它.这就是为什么许多其他语言中的编译时错误在 Dart 中是编译时警告和运行时错误的原因.这包括x is Foo",其中 Foo 不存在,使用不存在的类型进行类型注释,以及调用部分(抽象)类的构造函数.

Dart tries to allow you to run your program while you are developing it. That is why many things that are compile time errors in other languages are compile-time warnings and runtime errors in Dart. This includes "x is Foo" where Foo doesn't exist, type-annotating with a non-existing types, and calling constructors of partial (abstract) classes.

简而言之:因为这不是阻止程序编译的问题(不像语法错误可能意味着文件的其余部分被错误解释),所以没有理由阻止您运行代码.只有碰到真正依赖问题的分支,你的程序才会停止.

In short: because it's not a problem that prevents the program from being compiled (unlike a syntax error that might mean the rest of the file is interpreted wrongly), so there is no reason to stop you from running the code. Only if you hit the branch that actually depends on the problem will your program be stopped.

这篇关于为什么抽象类实例化不是 dart 中的运行时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆