gpt4 book ai didi

Scala 模式匹配守卫打破了模式匹配的穷尽性?

转载 作者:行者123 更新时间:2023-12-01 13:42:07 24 4
gpt4 key购买 nike

为什么在 Scala 中模式匹配中断模式匹配穷尽性时保护一个案例?

chris@chris-870Z5E-880Z5E-680Z5E:~/dev/bitcoins-core$ sbt console
[info] Loading project definition from /home/chris/dev/bitcoins-core/project
[info] Set current project to bitcoin-s-core (in build file:/home/chris/dev/bitcoins-core/)
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions to have them evaluated.
Type :help for more information.

scala> sealed trait A
defined trait A

scala> case class B() extends A
defined class B

scala> case class C() extends A
defined class C

scala> val b : A = B()
b: A = B()

scala> b match { case b : B => () }
<console>:15: warning: match may not be exhaustive.
It would fail on the following input: C()
b match { case b : B => () }
^

scala> b match { case b : B if b.isInstanceOf[B] => () }

scala>

请注意最后一行没有详尽警告,我是不是漏掉了什么?

最佳答案

看起来这是一个 open issue .

A comment by Adriaan Moors :

I understand it's disappointing, and it's not very intuitive why we bail out when there are guards, but our key design goal is not to have spurious warnings, as that would undermine their utility while annoying a lot of users.

关于Scala 模式匹配守卫打破了模式匹配的穷尽性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39170807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com