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

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

问题描述

VB具有运算符 AndAlso OrElse ,它们会执行短路强>逻辑合取.

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

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

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时使用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天全站免登陆