Home Add objects from case class in Scala

# Add objects from case class in Scala

Batman Begins
1#
Batman Begins Published in 2017-12-07 00:30:55Z
 So, I am new to programming and this might be a very rookie question. I need to add elements to an object. How is this done? scala> case class test ( a: String, b: String, c: Int, d: Int ) var teamData_Temp = test(lol, lel,1, 2) var teamData_All = teamData_Temp  Then let's say that teamData_Temp becomes teamData_temp = test(kok,kek,3,4) at some point in the script How do I produce teamData_All = (test(lol,lel,1,2),test(kok,kek,3,4)) ???
 Scala is a statically typed language. This means that although variables declared with var can be replaced with a new value, the type of that variable cannot change over the lifetime of the program. The type of teamData_All in your script is test while the type of teamData_All that you are trying to change it to is Tuple2[test, test] (or equivalently (test, test)). These are incompatible types. If you want to have teamData_All be growable, you have to specify that sort of behavior upfront when declaring the variable, e.g. by making it a list of type List[test]. Then you could have one line set teamData_All = List(test(lol,lel,1,2)) and then have another line set teamData_All = List(test(lol,lel,1,2),test(kok,kek,3,4)). The reason why this is necessary for a statically typed language is because if you make references to teamData_All in multiple places in your code, there is no way for the compiler to statically, i.e. before running your code, know what the type of teamData_All is at a given point, since you might have changed its type. This makes it impossible for the compiler to make its usual guarantees (e.g. making sure you're only calling methods on things that actually have that method defined).