Variable values ​​in an object

In Scala, if I plan on having a mutable attribute (like a bag of numbers) of an object when appropriate

  • Create var and use immutable data structure?
  • Create val and use mutable data structure?

I'm going to guess what you want to use # 2 for streaming applications? Are some of the kits thread safe?

How at all? (Or is it not a big deal?)

+3
source share
2 answers

1 2 - mutable , , , , a var val . .

+1

, ...

1 2 - val . - Java java.util.concurrent. , :

def emptyConcurrentHashMap[K, V] = {
  import collection.JavaConverters._
  new java.util.concurrent.ConcurrentHashMap[K, V].asScala
}

Scala, , (, def putIfAbsent(k: A, v: B): Option[B] def replace(k: A, oldvalue: B, newvalue: B): Boolean).

AtomicReference, , compareAndSet.

+2

All Articles