重新分配给 Scala 中的 val
我正在 Scala 中进行训练练习并收到此 val 重新分配错误。我不知道在哪里为 val 重新分配新值
class personTest
{
val alf = Person("Alf", 30, List(EmailAddress("[email protected]")))
val fredrik = Person("Fredrik", 33, List(EmailAddress("[email protected]"), EmailAddress("[email protected]")))
val johannes = Person("Johannes", 0, Nil)
val persons = List(alf, fredrik, johannes)
@Test
def testNameToEmailAddress
{
// Create a map from each persons name to their e-mail addresses,
// filtering out persons without e-mail addresses
// Hint: First filter list, then use foldLeft to accumulate...
val emptyMap: Map[String, List[EmailAddress]] = Map()
val nameToEmail = persons.filter(_.emailAddresses.length>0).foldLeft(emptyMap)((b,p)=> b+=p.name->p.emailAddresses)
assertEquals(Map(alf.name -> alf.emailAddresses, fredrik.name -> fredrik.emailAddresses), nameToEmail)
}
}
,并且收到此错误
error: reassignment to val
val nameToEmail = persons.filter(_.emailAddresses.length>0).foldLeft(emptyMap)((b,p)=> b+=p.name->p.emailAddresses)
I am doing a training exercise in Scala and getting this val reassignment error. I don't see where I am reassigning a new value to a val
class personTest
{
val alf = Person("Alf", 30, List(EmailAddress("[email protected]")))
val fredrik = Person("Fredrik", 33, List(EmailAddress("[email protected]"), EmailAddress("[email protected]")))
val johannes = Person("Johannes", 0, Nil)
val persons = List(alf, fredrik, johannes)
@Test
def testNameToEmailAddress
{
// Create a map from each persons name to their e-mail addresses,
// filtering out persons without e-mail addresses
// Hint: First filter list, then use foldLeft to accumulate...
val emptyMap: Map[String, List[EmailAddress]] = Map()
val nameToEmail = persons.filter(_.emailAddresses.length>0).foldLeft(emptyMap)((b,p)=> b+=p.name->p.emailAddresses)
assertEquals(Map(alf.name -> alf.emailAddresses, fredrik.name -> fredrik.emailAddresses), nameToEmail)
}
}
and I am getting this error
error: reassignment to val
val nameToEmail = persons.filter(_.emailAddresses.length>0).foldLeft(emptyMap)((b,p)=> b+=p.name->p.emailAddresses)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
b
是闭包参数的名称,它本身就是一个val
,不能重新分配。foldLeft
的工作原理是将一次闭包调用的返回值作为参数b
传递给下一个,因此您所需要做的就是 returnb + ( p.name->p.emailAddresses)
。 (不要忘记括号中的优先级。)b
which is the name of a parameter to your closure is itself aval
, which cannot be reassigned.foldLeft
works by taking passing the return value of one invocation of the closure as the parameterb
to the next, so all you need to do is returnb + (p.name->p.emailAddresses)
. (Don't forget the parentheses for precedence.)您正在表达式
b+=p.name->p.emailAddresses
中重新分配 valb
。You're reassigning val
b
in the expressionb+=p.name->p.emailAddresses
.不可变的
Map
没有+=
方法。在这种情况下,编译器会翻译b += p.name -> p.emailAddresses
到b = b + p.name->p.emailAddresses
。好了,重新分配!Immutable
Map
does not have a+=
method. In such case, compiler translatesb += p.name -> p.emailAddresses
tob = b + p.name->p.emailAddresses
. There you have it, reassignment!如前所述,错误消息源自表达式
...b+=bp.name...
但实际上,您根本不需要在这里执行 FoldLeft,这是一个简单的映射应该足够了。然后,任何
Seq[K->V]
都可以通过toMap
方法转换为Map[K,V]
。像这样的东西:
免责声明:未测试拼写错误等
As previously mentioned, the error message is originating in the expression
...b+=bp.name...
But really, you don't need to be doing a foldLeft here at all, a simple mapping should be enough. Any
Seq[K->V]
can then be converted to aMap[K,V]
via thetoMap
method.Something like this:
disclaimer: not tested for typos, etc.