我试图将输入数据解析为TreeSet中的case类,但它似乎不起作用。
case class Block(letter: Char)
// This does not compile
val brokenBlocks: collection.immutable.TreeSet[Block] = "A B C".split(' ').map(letter => Block(letter(0)))(collection.breakOut)
// Although this compiles
val workingBlocks: collection.immutable.TreeSet[Int] = "A B C".split(' ').map(letter => letter(0).asDigit)(collection.breakOut)编译错误:
type mismatch; found : scala.collection.generic.CanBuildFrom[Any,Char,String]
required: scala.collection.generic.CanBuildFrom[Array[String],Block,scala.collection.immutable.TreeSet[Block]]我正在scala工作表中尝试这一点。
发布于 2014-10-04 10:29:47
与案例类没有任何关系。如果您试图构建一个列表或向量,它就会工作得很好。
TreeSet需要对其元素的类型进行排序。有一个关于国际,但没有在布洛克。
如果您添加了一个,比如在同伴对象中。
object Block {
implicit val ordering: Ordering[Block] = Ordering.by(_.letter)
}那就成功了。
https://stackoverflow.com/questions/26191060
复制相似问题