问题描述
为什么一种语言不使用短路评估?不使用有什么好处吗?
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:
因为如果你的函数、属性 Gets 或操作符方法有副作用,它会表现不同并产生不同的结果.这可能与:A) 语言标准,B) 您的语言的先前版本,或 C) 您的语言典型用户的默认假设相冲突.这就是VB不短路的原因.
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.
因为您可能希望编译器可以自由地重新排序和修剪表达式、运算符和子表达式,而不是按照用户键入它们的顺序.这些是 SQLhas for not short-circuiting(或者至少不是大多数开发人员使用 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.
这篇关于为什么语言不使用短路评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!