我有以下方法:
def firstAndLast[CC, A, That](seq: CC)(implicit asSeq: CC => Seq[A], cbf: CanBuildFrom[CC, A, That]): That = {
val b = cbf(seq)
b += seq.head
b += seq.last
b.result
}
请参阅:方法采用 Seq[T] 返回 String 而不是 Seq[Char]以了解基本原理。它在第一种情况下就像一个魅力,但在第二种情况下无法编译:
List("abc", "def") map {firstAndLast(_)}
List("abc", "def") map firstAndLast
给予:
error: No implicit view available from CC => Seq[A].
List("abc", "def") map firstAndLast
知道如何改进此声明以避免额外的包装吗?似乎 eta-expansion 是问题所在(?)