为什么在dart中禁用assert函数? [英] why the assert function was disabled in dart?

查看:161
本文介绍了为什么在dart中禁用assert函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import 'dart:io';
main() {
  print("Enter an even number : ");
  int evenNo = int.parse(stdin.readLineSync());
  assert(evenNo % 2 == 0, 'wrong input');
  print("You have entered : $evenNo");
}

使此代码正常工作在执行assert函数之前,我必须使用'--enable-asserts'标签运行dart文件,而无需传递'--enable-asserts'标签.为什么禁用此功能?

to get this code to work properly I had to run the dart file with '--enable-asserts' tag and before assert function was executed without passing the '--enable-asserts' tag. why was this function disabled ?

推荐答案

什么是断言?

在包括Dart在内的许多语言中,断言"专门用于捕获逻辑错误.(Dart称这些 Error s.)这些是由于编程错误而导致的错误.这些类型的错误应该从不发生.从概念上讲,足够先进的静态分析器可以证明断言永远不会失败.在实践中,这种分析很困难,因此断言在实践中会得到验证.

What is an assertion?

In many languages, including Dart, "assertions" specifically are meant to catch logical errors. (Dart calls these Errors.) These are errors that are due to a programming mistake. These types of errors should never happen. Conceptually, a sufficiently advanced static analyzer could prove that assertions will never fail. In practice, such analysis is difficult, so assertions are verified at runtime as a matter of practicality.

这与运行时错误相反,后者是程序实际运行时发生的不可预测的错误.(Dart称这些 Exception s.)这些错误类型通常是由于无效的用户输入引起的,但它们还包括文件系统错误和硬件故障等.

This is in contrast to runtime errors, which are unpredictable errors that occur when the program is actually running. (Dart calls these Exceptions.) Often these types of errors are due to invalid user input, but they also include file system errors and hardware failures, among other things.

断言旨在用于在调试时验证假设(或捕获错误的假设),并且具有断言的编程语言通常允许将其禁用以用于生产(非调试)代码.由于断言在逻辑上永远不应该发生,因此没有必要花费额外的运行时间检查它们.由于可以禁用断言,因此也应避免使用不正确的断言.

Assertions are intended to be used to verify assumptions (or catch faulty ones) when debugging, and programming languages that have assertions typically allow them to be disabled for production (non-debug) code. Since assertions logically should never occur, there is no point in incurring the extra runtime cost of checking them. Since assertions can be disabled, that also should provide additional discouragement against using them improperly.

Dart选择默认禁用断言,因此您必须选择将其与-enable-asserts 一起使用.其他一些语言(例如C)则选择了退出系统.我不知道选择Dart的理由,但由于 assert s仅应用于调试,因此对我来说,像Dart这样的语言(通常可能会被解释)可以做到这一点用户更容易在生产模式下执行代码.相反,对于C之类的编译语言,启用或禁用断言的责任在于 developer 而不是用户.

Dart chose to leave assertions disabled by default, so you must opt-in to using them with --enable-asserts. Some other languages (e.g. C) chose an opt-out system instead. I don't know the rationale for this choice for Dart, but since asserts should be used only for debugging, it makes sense to me that a language like Dart (which often might be interpreted) makes it easier for users to execute code in production mode. In contrast, for compiled languages like C, the onus of enabling or disabling assertions is placed on the developer rather than on the user.

您的代码未正确使用 assert :您可以使用它来检查运行时输入.相反,这应该是您始终执行的检查,如果失败,则会产生运行时错误:

Your code does not use assert properly: You use it to check runtime input. That instead should be a check that you always perform and that produces a runtime error if it fails:

if (evenNo % 2 != 0) {
  throw FormatException('wrong input');
}

这篇关于为什么在dart中禁用assert函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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