为什么短路不是 VB 中的默认行为? [英] Why is short-circuiting not the default behavior in VB?

查看:19
本文介绍了为什么短路不是 VB 中的默认行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB 有运算符 AndAlsoOrElse,执行短路强>逻辑连接.

VB has operators AndAlso and OrElse, that perform short-circuiting logical conjunction.

为什么这不是 AndOr 表达式的默认行为,因为短路在每种情况下都很有用.

Why is this not the default behavior of And and Or expressions since short-circuiting is useful in every case.

奇怪的是,这与 &&|| 执行短路的大多数语言相反.

Strangely, this is contrary to most languages where && and || perform short-circuiting.

推荐答案

因为 VB 团队必须保持与旧代码(和程序员!)的向后兼容性

Because the VB team had to maintain backward-compatibility with older code (and programmers!)

如果短路是默认行为,按位操作会被编译器错误地解释.

If short-circuiting was the default behavior, bitwise operations would get incorrectly interpreted by the compiler.

AndAlso 和 OrElse 的民谣 由 Panopticon Central 提供

The Ballad of AndAlso and OrElse by Panopticon Central

我们的第一个想法是逻辑运算比位运算更常见,所以我们应该让 And 和 Or 成为逻辑运算符,并添加新的位运算符 BitAnd、BitOr、BitXor 和 BitNot(最后两个是为了完整性).然而,在其中一个测试版中,很明显这是一个非常糟糕的主意.VB 用户忘记了新运算符的存在,并在他的意思是 BitAnd 和 Or 的意思时使用 And,当他的意思是 BitOr 时,会得到可以编译但产生坏"结果的代码.

Our first thought was that logical operations are much more common than bitwise operations, so we should make And and Or be logical operators and add new bitwise operators named BitAnd, BitOr, BitXor and BitNot (the last two being for completeness). However, during one of the betas it became obvious that this was a pretty bad idea. A VB user who forgets that the new operators exist and uses And when he means BitAnd and Or when he means BitOr would get code that compiles but produces "bad" results.

这篇关于为什么短路不是 VB 中的默认行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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