为何会出现在`布尔?'不解除短路运营商? [英] Why are there no lifted short-circuiting operators on `bool?`?

查看:127
本文介绍了为何会出现在`布尔?'不解除短路运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不布尔支持解除&放大器;?&安培; || ?他们可以解除真正这将间接加入解除假运营商&放大器;&放; ||

Why doesn't bool? support lifted && and ||? They could have lifted the true and false operators which would have indirectly added lifted && and ||.

运营商 | &安培; 已经取消,实行正确的Three-valued逻辑。但是,当然,他们不是短路像 || &放大器;&安培;

The operators | and & are already lifted and implement the correct Three-valued logic. But of course they are not short circuiting like || and &&.

问题是,为什么他们决定不建立规范时解除的经营者。所以,是这样的,因为规范是这么说的是没有答案的为什么?

The question is why they decided not to lift those operators when creating the specification. So "It's like this because the spec says so" is no answer to the "why?".

起吊时真正既不是真正也不

When lifting true and false so that null is neither true nor false:

public static bool operator true(bool? x) 
{
    return x.HasValue && x.Value
}

public static bool operator false(bool? x) 
{
  return x.HasValue && !x.Value
}

这将导致&放大器;&安培; || 行为就像他们的非短路同行。除假放;&安培;什么真||什么会短路(真正在这两个例子中没有编译时间常数)。

This would have resulted in && and || behaving just like their non short-circuiting counterparts. Except that false && anything and true || anything would short circuit (false and true are no compile time constants in these two examples).

这将工作非常类似例子的DBBool MSDN上

我看提起这些运营商推出不足为奇或危险的行为。我错过了什么?

I see no surprising or dangerous behavior introduced by lifting these operators. Did I miss something?

我读<一个href=\"http://stackoverflow.com/questions/303181/nullablet-and-overloaded-operators-bool-bool\">another SO质疑这一点,但没有找到满意的答案。

I have read another SO question on this, but found none of the answers satisfying.

杰夫·耶茨的回答显示了一个很好的理由,为什么解除真正 / 运营商是不是最佳的,它并不能解释为什么解除&放大器;&安培; || 直接是坏的。由于运营商提升是编译器魔术在特殊情况下可空&LT; T&GT; 它并不需要遵循正常的类型超载规则,因此将能够提供&放大器;&安培; / || 没有解除真正

Jeff Yates's answer shows a nice reason for why lifting the true/false operators isn't optimal, it doesn't explain why lifting && and || directly is bad. Since operator lifting is compiler magic that special cases Nullable<T> it doesn't need to follow the overloading rules for normal types and thus would be able to offer &&/|| without lifting true.

推荐答案

既然你表明解除真正在技术上是可能的,只有两到你的问题可能的答案(用他们是谁写的编译器/规范人):

Since you showed that lifting true and false is technically possible, there are only two possible answers to your question (with "they" being the people who wrote the compiler/spec):


  1. 这是在规范,也就是一个错误。他们没有想到这一点。 (可能的,但我怀疑)

  2. 他们认为,解除短路运营商可能是容易出错。这可能是推理的方式一样,为什么C#是完全基于类的(没有唯一的功能在C ++)或为什么像的声明,如果(myNullVar){...} (与 myNullVar 作为一个参考)在C#中不工作(但它在C / C ++)。

  1. It's an error in the spec, ie. they didn't think of this. (possible, but I doubt that)
  2. They thought that lifting the short-circuiting operators is potentially error-prone. It could be the same way of reasoning as why C# is completely class based (no sole functions as in C++) or why a statement like if (myNullVar) { ... } (with myNullVar being a reference) doesn't work in C# (but it does in C/C++).

我觉得总有使编程语言更强大,使两者之间取得平衡是不容易出错。

I think there's always a balance between making a programming language more powerful and making it less error-prone.

更新:只为你的兴趣,这是什么的官方文档说:

Update: Just for you interest, that's what the official documentation says:

这是不允许的,因为它是目前还不清楚是指在有条件的情况下什么空。

This is not allowed because it is unclear what null means in the context of a conditional.

这篇关于为何会出现在`布尔?'不解除短路运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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