为什么“bool”上没有提升的短路运算符?
为什么 bool?
不支持提升的 &&
和 ||
?他们本可以提升 true
和 false
运算符,从而间接添加提升的 &&
和 ||
。
运算符 |
和 &
已被提升并实现正确的 三值逻辑。当然,它们并不像 ||
和 &&
那样短路。
问题是为什么他们决定在创建规范时不取消这些操作员。所以“它是这样的,因为规范是这么说的”并不是“为什么?”的答案。
当提升 true
和 false
以使 null
既不是 true
也不是 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
}
时 :会导致 &&
和 ||
的行为就像它们的非短路对应项一样。除了 false &&任何内容
和true ||任何东西都会短路(在这两个示例中,
false
和 true
不是编译时常量)。
这与 MSDN 上的 DBBool 示例非常相似。
我认为提升这些操作员不会带来任何令人惊讶或危险的行为。我错过了什么吗?
我已经阅读了另一个SO问题,但没有找到令人满意的答案。
Jeff Yates 的回答给出了为什么提升 true
/false
运算符不是最佳选择的一个很好的理由,它没有解释为什么提升 && 和
||
直接是不好的。由于运算符提升是编译器的魔法,特殊情况 Nullable
它不需要遵循普通类型的重载规则,因此能够提供 &&
>/||
而不提升 true
。
Why doesn't bool?
support lifted &&
and ||
? They could have lifted the true
and false
operators which would have indirectly added lifted &&
and ||
.
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).
This would work very similar to the DBBool example on MSDN.
I see no surprising or dangerous behavior introduced by lifting these operators. Did I miss something?
I have read another SO question on this, but found none of the answers satisfying.
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的建议将为可为空类型创建两种不同的使用模式。
请考虑以下代码:
为了保持可空类型使用的一致性,不要在
bool
上提升true
和false
运算符是有意义的。我不知道这是否是未完成的真正原因,但这对我来说是有道理的。What you propose would create two different usage patterns for nullable types.
Consider the following code:
For consistency in the usage of nullable types, it makes sense to not lift the
true
andfalse
operators onbool
. I don't know if this is the real reason why it wasn't done, but it makes sense to me.由于您表明提升
true
和false
在技术上是可能的,因此您的问题只有两个可能的答案(“他们”是编写编译器/规范的人) :if (myNullVar) { ... }
这样的语句(使用myNullVar
作为参考)在 C# 中不起作用(但在 C/C++ 中起作用)。我认为在让编程语言变得更强大和减少出错之间总是需要取得平衡。
更新:只是为了您的兴趣,这就是官方文档 说:
Since you showed that lifting
true
andfalse
is technically possible, there are only two possible answers to your question (with "they" being the people who wrote the compiler/spec):if (myNullVar) { ... }
(withmyNullVar
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:
假&& everything
与false
相同。但是,如果您期望false &&仅当
才为 true,那么anything
为 false 时,anything!anything
就是您想要的。另外,
true || everything
与true
相同。 ...而且我不确定如何让它在任何条件下返回 false,因为让“这个或那个”什么都不返回是没有意义的!...既然条件如此简单明了,为什么还要增加额外的权重呢?
我通常不擅长“因为就是这样”,但我看不到添加此类功能的优势。
false && anything
is the same asfalse
. However if you expectfalse && anything
to be true only ifanything
is false, then!anything
is what you want.Also,
true || anything
is the same astrue
. ...And I'm not sure how you could have it return false on any condition, as it would make no sense to have "this or that" return nothing!... why adding additional weight to the condition when it's all clear and simple as it is?
I am not usually an adept of "because this is so", but I fail to see the advantage of adding such functionality.