Hello,
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)