【发布时间】:2020-04-12 22:44:18
【问题描述】:
我创建了一个不可变列表并尝试将其折叠到一个映射中,其中每个元素都映射到一个常量字符串“abc”。我这样做是为了练习。 当我这样做时,我遇到了一个错误。我不确定为什么地图(这里是具有可变地图类型的 e1)被转换为 Any。
val l = collection.immutable.List(1,2,3,4)
l.fold (collection.mutable.Map[Int,String]()) ( (e1,e2) => e1 += (e2,"abc") )
l.fold (collection.mutable.Map[Int,String]()) ( (e1,e2) => e1 += (e2,"abc") )
<console>:13: error: value += is not a member of Any
Expression does not convert to assignment because receiver is not assignable.
l.fold (collection.mutable.Map[Int,String]()) ( (e1,e2) => e1 += (e2,"abc") )
【问题讨论】:
标签: scala functional-programming