为什么一种语言不使用短路评估? [英] Why would a language NOT use Short-circuit evaluation?

查看:74
本文介绍了为什么一种语言不使用短路评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么一种语言不使用短路评估?不使用它有什么好处?

Why would a language NOT use Short-circuit evaluation? Are there any benefits of not using it?

我看到这可能会导致某些性能问题……是真的吗?为什么?

I see that it could lead to some performances issues... is that true? Why?

相关问题:使用短路评估的好处

推荐答案

不使用短路评估的原因:

Reasons NOT to use short-circuit evaluation:

  1. 因为如果您的函数,属性Get或运算符方法具有副作用,它将表现不同并产生不同的结果.这可能与以下内容冲突:A)语言标准,B)语言的先前版本,或C)典型用户的默认语言假设.这就是VB不短路的原因.

  1. Because it will behave differently and produce different results if your functions, property Gets or operator methods have side-effects. And this may conflict with: A) Language Standards, B) previous versions of your language, or C) the default assumptions of your languages typical users. These are the reasons that VB has for not short-circuiting.

因为您可能希望编译器自由地按其认为合适的方式对表达式,运算符和子表达式进行重新排序和修剪,而不是按照用户键入它们的顺序进行.这就是SQL的原因.不会短路(或者至少不会以大多数使用SQL的开发人员认为会短路的方式).因此,SQL(和其他一些语言)可能会短路,但前提是它决定并且不一定按照您隐式指定的顺序进行.

Because you may want the compiler to have the freedom to reorder and prune expressions, operators and sub-expressions as it sees fit, rather than in the order that the user typed them in. These are the reasons that SQL has for not short-circuiting (or at least not in the way that most developers coming to SQL think it would). Thus SQL (and some other languages) may short-circuit, but only if it decides to and not necessarily in the order that you implicitly specified.

我在这里假设您正在询问自动的,隐式的特定于订单的短路",这是大多数开发人员对C,C ++,C#,Java等的期望.VB和SQL都具有显式 force 特定于订单的短路.但是,通常当人们问这个问题时,这是一个按我的意思做"的问题.也就是说,它们的意思是为什么不按我的意愿做?",按照我写的顺序自动短路.

I am assuming here that you are asking about "automatic, implicit order-specific short-circuiting", which is what most developers expect from C,C++,C#,Java, etc. Both VB and SQL have ways to explicitly force order-specific short-circuiting. However, usually when people ask this question it's a "Do What I Meant" question; that is, they mean "why doesn't it Do What I Want?", as in, automatically short-circuit in the order that I wrote it.

这篇关于为什么一种语言不使用短路评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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