Scala 为什么类型检查会引发警告

Scala 为什么类型检查会引发警告,scala,Scala,我试图检查地图上的所有值是否都是Plant类型。我知道有更好的方法可以做到这一点。但我正在努力理解这场比赛,我想知道为什么这不起作用。我在这里犯了什么错误 为什么会有警告呢。ab2拥有所有的植物,但为什么它不能返回真值 abstract class Living abstract class Animal extends Living abstract class Plant extends Living case class Dog() extends Animal cas

我试图检查地图上的所有值是否都是Plant类型。我知道有更好的方法可以做到这一点。但我正在努力理解这场比赛,我想知道为什么这不起作用。我在这里犯了什么错误

为什么会有警告呢。ab2拥有所有的植物,但为什么它不能返回真值

 abstract class Living
  abstract class Animal extends Living
  abstract class Plant extends Living

  case class Dog() extends Animal
  case class Hibiscus() extends Plant
  case class Apple() extends Plant

  val ab1  = Map(1 -> Dog, 2 -> Hibiscus)
  val ab2  = Map(1 -> Apple, 2 -> Hibiscus)

  val isPlant = ab2.forall((x) => x match {

    case (x: Int, p: Plant) => true
    case  _ => false
  })

  print(isPlant)
输出

Solution.scala:18: warning: fruitless type test: a value of type scala.runtime.AbstractFunction0[Solution.Plant with Product with Serializable] cannot also be a Solution.Plant
    case (x: Int, p: Plant) => true
                     ^
one warning found
false
编辑:如果我明确定义了类型,我可能会发现这个问题

 abstract class Living
  abstract class Animal extends Living
  abstract class Plant extends Living

  case class Dog() extends Animal
  case class Hibiscus() extends Plant
  case class Apple() extends Plant

  val ab1: Map[Int,Living]  = Map(1 -> Dog, 2 -> Hibiscus)
  val ab2: Map[Int,Living]  = Map(1 -> Apple, 2 -> Hibiscus)

  val isPlant = ab2.forall((x) => x match {

    case (x: Int, p: Plant) => true
    case  _ => false
  })

  print(isPlant)
错误:

Solution.scala:13: error: type mismatch;
 found   : Solution.Dog.type
 required: Solution.Living
  val ab1: Map[Int,Living]  = Map(1 -> Dog, 2 -> Hibiscus)
                                       ^
Solution.scala:13: error: type mismatch;
 found   : Solution.Hibiscus.type
 required: Solution.Living
  val ab1: Map[Int,Living]  = Map(1 -> Dog, 2 -> Hibiscus)
                                                 ^
Solution.scala:14: error: type mismatch;
 found   : Solution.Apple.type
 required: Solution.Living
  val ab2: Map[Int,Living]  = Map(1 -> Apple, 2 -> Hibiscus)
                                       ^
Solution.scala:14: error: type mismatch;
 found   : Solution.Hibiscus.type
 required: Solution.Living
  val ab2: Map[Int,Living]  = Map(1 -> Apple, 2 -> Hibiscus)
                                                   ^
four errors found

您的地图不包含植物。它包含调用时返回植物的函数。您可以通过在地图中写入
Dog()
等来应用它们,或者更好的是,将植物定义为案例对象而不是类(这样就不需要应用它们)。

您的地图不包含植物。它包含调用时返回植物的函数。您可以通过在地图中写入
Dog()
等来应用它们,或者更好的是,将植物定义为案例对象而不是类(这样就不需要应用它们).

案例类
自动生成的伴生
对象
不扩展其伴生
,也不是其实例

换句话说,
Hibiscus
伴生对象不扩展
案例类Hibiscus()
,也不是
案例类Hibiscus()
的实例,因此它也不是
水果的实例


由于
Hibiscus
是一个常量,编译器知道它在运行时将是什么类型,并警告您它永远无法匹配您正在测试的类型,因此,模式的分支是不可访问的。

案例类
自动生成的伴生
对象
不会扩展其伴生
,也不是它的实例

换句话说,
Hibiscus
伴生对象不扩展
案例类Hibiscus()
,也不是
案例类Hibiscus()
的实例,因此它也不是
水果的实例


由于
Hibiscus
是一个常量,编译器知道它在运行时将是什么类型,并警告您它永远无法匹配您正在测试的类型,因此无法访问该模式的分支。

谢谢!!sepp2k和jorg的回答都提供了信息。我选择了jorg的一个,因为它解释了为什么会出现警告。我对sepp2k的评论投了赞成票,因为它增加了决议。谢谢!!sepp2k和jorg的回答都提供了信息。我选择了jorg的一个,因为它解释了为什么会出现警告。我对sepp2k的评论投了赞成票,因为它增加了决议。