Scala - 布尔值 - &与&&, |与 || [英] Scala - Booleans - & vs &&, | vs ||

查看:23
本文介绍了Scala - 布尔值 - &与&&, |与 ||的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚注意到,在 Scala 中,Boolean 支持 &&&.这两个运营商有区别吗?Scala 文档对它们都使用了完全相同的描述,所以我不确定.

I just noticed that in Scala Boolean supports both & and &&. Is there a difference between these two operators? The Scala docs use the exact same description for both of them, so I wasn't sure.

推荐答案

&| 是严格的,而 &&|| 正在短路:

& and | are strict while && and || are short-circuiting:

false && (throw new Exception()) => false
false & (throw new Exception()) => ex

true || (throw new Exception()) => true
true | (throw new Exception()) => ex

&| 的完整文档有一个解释此行为的注释:

The full documentation for & and | have a note explaining this behaviour:

此方法同时计算 a 和 b,即使结果已经是评估后确定.

This method evaluates both a and b, even if the result is already determined after evaluating a.

这篇关于Scala - 布尔值 - &与&&, |与 ||的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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