为什么“bool”上没有提升的短路运算符?

发布于 2024-10-20 11:15:40 字数 1601 浏览 6 评论 0原文

为什么 bool? 不支持提升的 &&||?他们本可以提升 truefalse 运算符,从而间接添加提升的 &&||

运算符 |& 已被提升并实现正确的 三值逻辑。当然,它们并不像 ||&& 那样短路。

问题是为什么他们决定在创建规范时不取消这些操作员。所以“它是这样的,因为规范是这么说的”并不是“为什么?”的答案。

当提升 truefalse 以使 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 ||任何东西都会短路(在这两个示例中,falsetrue 不是编译时常量)。

这与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

御守 2024-10-27 11:15:40

您的建议将为可为空类型创建两种不同的使用模式。

请考虑以下代码:

bool? a = null;

// This doesn't currently compile but would with lifted true/false operators.
if (a)
{
}

// Whereas this provides a consistent use of nullable types.
if (a ?? false)
{
}

为了保持可空类型使用的一致性,不要在 bool 上提升 truefalse 运算符是有意义的。我不知道这是否是未完成的真正原因,但这对我来说是有道理的。

What you propose would create two different usage patterns for nullable types.

Consider the following code:

bool? a = null;

// This doesn't currently compile but would with lifted true/false operators.
if (a)
{
}

// Whereas this provides a consistent use of nullable types.
if (a ?? false)
{
}

For consistency in the usage of nullable types, it makes sense to not lift the true and false operators on bool. I don't know if this is the real reason why it wasn't done, but it makes sense to me.

默嘫て 2024-10-27 11:15:40

由于您表明提升 truefalse 在技术上是可能的,因此您的问题只有两个可能的答案(“他们”是编写编译器/规范的人) :

  1. 这是规范中的错误,即。他们没有想到这一点。 (有可能,但我对此表示怀疑)
  2. 他们认为解除短路运算符可能容易出错。这可能与为什么 C# 完全基于类(没有 C++ 中的唯一函数)或为什么像 if (myNullVar) { ... } 这样的语句(使用 myNullVar 作为参考)在 C# 中不起作用(但在 C/C++ 中起作用)。

我认为在让编程语言变得更强大和减少出错之间总是需要取得平衡。

更新:只是为了您的兴趣,这就是官方文档 说:

这是不允许的,因为不清楚 null 在条件上下文中意味着什么。

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. 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.

瞳孔里扚悲伤 2024-10-27 11:15:40

假&& everythingfalse 相同。但是,如果您期望 false &&仅当 anything 为 false 时,anything 才为 true,那么 !anything 就是您想要的。

另外,true || everythingtrue 相同。 ...而且我不确定如何让它在任何条件下返回 false,因为让“这个或那个”什么都不返回是没有意义的!

...既然条件如此简单明了,为什么还要增加额外的权重呢?

我通常不擅长“因为就是这样”,但我看不到添加此类功能的优势。

false && anything is the same as false. However if you expect false && anything to be true only if anything is false, then !anything is what you want.

Also, true || anything is the same as true. ...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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文