我正在学习 scala,作为最好的培训,我正在将旧的 Java 算法转换为函数式编程风格。我有以下代码:
def test(originalSet: Set[Int]):Boolean = originalSet match {
case Set() => true
case x::y => false
}
此代码适用于 Lists,但对于 Sets,它会给我以下编译错误:
- value Set is not a case class constructor, nor does it have an unapply/unapplySeq
method
和
- constructor cannot be instantiated to expected type; found : scala.collection.immutable.::[B] required:
scala.collection.immutable.Set[Int]
- constructor cannot be instantiated to expected type; found : scala.collection.immutable.::[B] required:
scala.collection.immutable.Set[Int]
问题是什么?如何测试 Set 为空的情况?当 set 有头部和尾部时,我该如何处理?