I have a class that needs to be written to a file to interact with any inherited C ++ application. Since it will be created several times at the same time, it is a good idea to give the file a unique name.
I could use System.currentTimemili or hashcode, but there is a chance of conflict. Another solution is to place the field varinside the companion object.
As an example, the code below shows one such class with the latest solution, but I'm not sure if this is the best way to do this (at least it seems thread safe):
case class Test(id:Int, data: Seq[Double]) {
//several methods writing files...
}
object Test {
var counter = 0
def new_Test(data: Seq[Double]) = {
counter += 1
new Test(counter, data)
}
}
user445107
source
share