Rajeun
Rajeun

Reputation: 721

reassignment to val in scala when trying to update an object

I'm trying to add an object to a sub list (thirdList) in scala so I have a first loop where I loop on the first object and a second loop where I loop on a second sub list when finding the object I try to add my new object to a list (thirdList) this is my code:

        if (!firstList.isEmpty) {
          for (firstObject <- firstList) {
            if (firstObject.rank == firstObjectRank) {
              println("ok")
              var secondList = firstObject.secondList
              if (!secondList.isEmpty) {
                for (secondObject <- secondList) {
                  if (secondObject.rank == secondObjectRank) {
                    println("ok 2")
secondObject.thirdList = secondObject.thirdList :+ myNewObject
                  } else {
                    println("secondObject not found")
                  }
                }
              }
            } else {
              println("firstObject not found")
            }
          }
        } else {
          println("firstList is empty")
        }

Here is my structure:

{
"firstList": [
{ "firstObject" : "ObjectFirstList"},
{
"secondList" : [
{"secondObject" : "ObjectSecondList"},
"thirdList" : [
{"thirdObject" : "ObjectThirdList"}
]
]
}
]
}

Upvotes: 0

Views: 6300

Answers (1)

Tomasz Błachut
Tomasz Błachut

Reputation: 2507

In functional languages, like Scala, you should use values and immutable data structures. They cannot be updated, which leads to many useful characteristics - explanation of such is beyond this question.

Keyword val represents value. Once assigned, you cannot reassign to it, i.e. use = operator again:

secondObject.thirdList = {...}

Keyword var represents variable. You can reassign to it freely. As a side note, variables and values (in other languages known as constants) in Scala intentionally differ by one character in order to encourage people to use the latter.

To be thorough I should mention that List in Scala is immutable, and

secondObject.thirdList :+ myNewObject

returns a new object rather than updating existing one

Enough with theory. Your class definition probably looks something like this:

class A {
  val thirdList = List()
}

Notice that you assigned something to val with =

You have 2 options here:

  1. Replace val with var - your snippet will work as is because var allow reassignment
  2. Replace List with some mutable data structure, i.e. MutableList. At this point your code still won't work because mutation of structure and reassignment to value are two different things. Usage of = is still illegal. You can instead replace whole line with

    secondObject.thirdList += myNewObject
    

    Second options is better IMHO.

BTW your code can be greatly shortened with the power of Scala Collections. Take a look at them, for example start with:

firstList.find(object => object.rank == firstObjectRank).map(_.secondList).find(object => object.rank == secondObjectRank)

Upvotes: 2

Related Questions