Scalastyle 布尔表达式可以简化 [英] Scalastyle Boolean expression can be simplified

查看:47
本文介绍了Scalastyle 布尔表达式可以简化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scalastyle(intellij 2016.1 默认值)说这个布尔表达式可以简化

Scalastyle (intellij 2016.1 defaults) says this boolean expression can be simplified

val t = Option(true)
val f = Option(false)
if(t.contains(true) && f.contains(false)) {
  println("booop")
}

我可以通过将 if 更改为:

I can get rid of this by changing the if to:

if(t.contains(true).&&(f.contains(false)))

或者通过更改 &&&

Or by changing && to &

但并没有真正看到这是如何简化它的,谁能解释一下发生了什么?

But not really seeing how this is simplifying it, could anyone explain what's going on?

更新它似乎与 vals 在编译时是否已知或它们是否在本地定义无关.下面的代码也得到了表达式可以简化的警告:

Update It doesn't appear to be related to if the vals are known at compile time, or them being locally defined. The following code also get's the warning that the expression can be simplfied:

object TestFoo {
  def bar(t: Option[Boolean]) = {
    val f = Option(scala.util.Random.nextBoolean)
    if (t.contains(true) && f.contains(false)) println("booop")
  }
  def main(args: Array[String]) = bar(t = Option(scala.util.Random.nextBoolean))
}

我只是不明白我应该如何使它更简单,是否有一些奇怪的 Option[Boolean] 比较我错过了?

I just don't get how I'm supposed to make that any simpler, is there some strange Option[Boolean] comparing I'm missing out on?

推荐答案

它似乎建议您与方法调用用法保持一致.中缀形式的所有内容:

It seems to suggest you being consistent with the method call usage. Either everything in infix form:

(t contains true) && (f contains false)

或者常规方法调用形式的所有内容:

Or everything in regular method call form:

t.contains(true).&&(f.contains(false))

这篇关于Scalastyle 布尔表达式可以简化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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