lundi 7 novembre 2016

Access the name of a value's parent naming from within the instantiated class (Scala)?

Assume Scala 2.11. I'm writing a class that will persist a Scala value. It's intention is to be used as such:

class ParentClass {

  val instanceId: String = "aUniqueId"

  val statefulString: Persisted[String] = persisted { "SomeState" }

  onEvent {
    case NewState(state) => statefulString.update(state)
  }

}

Persisted is a class with a type parameter that is meant to persist that specific value like a cache, and Persist handles all of the logic associated with persistence. However, to simply the implementation, I'm hoping to retrieve information about it's instantiation. For example, if it's instance in the parent class is named statefulString, how can I access that name from within the Persisted class itself?

The purpose of doing this is to prevent collisions in automatic naming of persisted values while simplifying the API. I cannot rely on using type, because there could be multiple values of String type.

Thanks for your help!





Aucun commentaire:

Enregistrer un commentaire