我偶然发现了Predef.StringCanBuildFrom
令人惊讶的实现,这打破了我CanBuildFrom
在代码中所做的假设.这是实施:
implicit def stringCanBuildFrom: CanBuildFrom[String, Char, String] = new CanBuildFrom[String, Char, String] { def apply(from: String) = apply() def apply() = mutable.StringBuilder.newBuilder }
apply(String)
简单地忽略参数似乎完全不自然.对我来说,正确的实施应该是
implicit def stringCanBuildFrom: CanBuildFrom[String, Char, String] = new CanBuildFrom[String, Char, String] { def apply(from: String) = apply() ++= from def apply() = mutable.StringBuilder.newBuilder }
但这似乎是微不足道的,以至于我无法相信我是那个因为语言存在而发现它的人.我很想为此开一个问题,但是如果我错过任何没有理由不做我的建议,请告诉我!