Scala 隐式编程:错误信息优化、幻影类型与隐式转换
优化错误信息
在 Scala 中使用集合 API 和 CanBuildFrom 时,如果尝试对没有对应 CanBuildFrom 定义的自定义目标类型使用 map 方法,会出现特定错误。例如:
scala> case class ListWrapper(list: List[Int])
defined class ListWrapper
scala> List(1,2,3).map[Int,ListWrapper](_ * 2)
<console>:10: error: Cannot construct a collection of type ListWrapper
with elements of type Int based on a collection of type List[Int].
List(1,2,3).map[Int,ListWrapper](_*2)
^
这里 map[Int,ListWrapper] 明确指定了输出类型为 ListWrapper ,触发了错误。错误信息 “Cannot construct a collection of type ListWrapper with elements of type Int based o
超级会员免费看
订阅专栏 解锁全文
71

被折叠的 条评论
为什么被折叠?



