2

How would I access existing variable data in Scala. I'll give an example:

Class storage: Has a map (and other data which I use)

In class A:

val storageManager = new storage();
storageManager.putValue("key","value");
println("A: " + storageManager.getValue("key"));

In class B:

val storageManager2 = new storage();
println("B: " + storageManager2.getValue("key"));

Results are:

A: value

B: null

I'd like to get class B to print "value" as well i.e. make the contents in the storage class the same for all classes accessing them.

Is passing the storeManager variable the only way? as I would wish to avoid that

2 Answers 2

4

You really should pass the variable. It makes for much more maintainable code in the long run, as it is then explicit that these two classes are using the same storage manager (and lets you change that if you want to).

But if you want to use shared storage, just create one in a singleton object and refer to that.

object StorageForAB {
  val storageManager = new storage
}

class A {
  def storageManager = StorageForAB.storageManager
}
class B {
  def storageManager2 = StorageForAB.storageManager
}

(I've changed val to def because there's no reason to copy the variable--just refer to the common copy.)

Note that this will prevent garbage collection of the storageManager. And note also that this will share the same storageManager across all instances of A and B. If you don't want that, I'm not sure how you expect any given A to be paired with an appropriate B if you don't say so (e.g. by passing them the same storage).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It's exactly what I wanted.
2

A solution which is a little more flexible (if you should need it), is the following:

class Storage(key:String) // Just to make the code work, this could be anything

object StorageManager {
    private val instances = scala.collection.mutable.Map[String, Storage]()

    def apply(key:String) = instances.get(key) match {
        case Some(x) => x
        case _ =>
            val instance = new Storage(key)
            instances.put(key, instance)
            instance
    }
}

This would allow you to create your storage classes based on an identifier, and then re-use them at will. For example, in your classes A and B, instead of new storage(); you'd do:

val storageManager = StorageManager("SharedClassAB");

Invoking the StorageManager's apply method with the same key-value always yields the same Storage instance, which is created in case it does not exist yet.


I'm not sure if this is actually something you might need, but since it was the first thing that came to mind I've posted it. If you don't need that level of flexibility, you should probably go with @RexKerr's solution :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.