我想將地圖從任何值的類型轉換爲斯卡拉的字符串。轉換地圖中的值的類型 - 斯卡拉
我有這樣
import scala.collection.generic.CanBuildFrom
case class Name(FristName: String,LastName: String)
object App29 extends App {
val name = Name("mark","aden")
val stringListBuilder = new CanBuildFrom[List[Any], String, List[String]] { def apply(from: List[Any]) = this.apply(); def apply() = List.newBuilder[String] }
val result = name.getClass.getDeclaredFields.map(_.getName)
.zip(name.productIterator.toList)(stringListBuilder).toMap
}
代碼,所以我使用CanBuilderForm基本上從地圖[字符串,任何]轉換返回類型地圖[字符串,字符串]但我不明白爲什麼它拋出類型不匹配錯誤。
感謝
編輯-1
我想通了,我可以用name.productIterator.map(_。的toString),將其轉換爲字符串,但我想知道爲什麼我的CanBuildFrom語法是錯誤的如何。
這個[Scala-lang博客](https://www.scala-lang.org/blog/2017/05/30/tribulations-canbuildfrom.html)可能會對你感興趣。實質上,Scala將在其正在進行的語言設計中使用'CanBuildFrom'。 –