小编Ami*_*sad的帖子

Scala - 值<不是AnyVal的成员

这是我的代码.我在第二个案例中遇到了编译错误:

错误:(92,26)值<不是AnyVal案例x的成员,如果x._2 <2 =>"价格低于2"

def tupleMatch: Unit = {
  val donut = Tuple2("Donut", 2.5)
  val plain = Tuple2("Plain", 1.0)
  val tuples = List(donut, plain)
  tuples.foreach { tuple => 
    val toPrint = tuple match {
      case ("Donut", price) => s"price of Donut is ${donut._2}"
      case (_, price) if price < 2 => "price under 2"
      case _ => "other"
    }
    println(toPrint)

  }
}
Run Code Online (Sandbox Code Playgroud)

我改变之后

val plain = Tuple2("Plain", 1)
Run Code Online (Sandbox Code Playgroud)

val plain = Tuple2("Plain", 1.0)
Run Code Online (Sandbox Code Playgroud)

它终于奏效了.这让我感到困惑,我想知道为什么?

functional-programming scala

5
推荐指数
2
解决办法
368
查看次数

标签 统计

functional-programming ×1

scala ×1