【发布时间】:2018-12-20 07:22:58
【问题描述】:
我正在尝试以下代码:
val set1 = Set(1,2,3,4,5,67,8)
val TRUE_BOOLEAN = true
val FALSE_BOOLEAN = false
set1.contains(4) match {
case TRUE_BOOLEAN => println("Element found")
case FALSE_BOOLEAN => println("Element not found")
}
但是,当我尝试在IntelliJ 中运行它时,它在Messages 选项卡中给出以下警告:
Warning:(11, 16) match may not be exhaustive.
It would fail on the following inputs: false, true
set1.contains(4) match {
然而,如果我使用 true 和 false 而不是 TRUE_BOOLEAN 和 FALSE_BOOLEAN,我不会收到任何警告。
set1.contains(4) match {
case true => println("Element found")
case false => println("Element not found")
}
谁能解释这个警告的原因以及为什么它会随着true和false消失。
【问题讨论】:
-
即使我明确指定类型,我仍然会收到警告。无论类型注释是否存在,Scastie 也会给出相同的警告。不可重现。考虑将其明确重新标记为 intellij-bug。
-
@Yogesh 这与您的提议有何相同之处?
-
@AndreyTyukin 如提到的重复 URL,
in pattern matching you should account for all possible cases or provide a "fallback"如果您在匹配中添加case _,则不会显示警告。 -
@AndreyTyukin 是的,你是对的......即使在明确添加类型后我也会收到警告。我将编辑我的问题。