How do I specify type parameters via a config file?

I am creating a market simulator using Scala / Akka / Play. I have an Akk actor with two children. Children should have specific types that I would like to specify as parameters.

Suppose I have the following class definition ...

case class SecuritiesMarket[A <: AuctionMechanismLike, C <: ClearingMechanismLike](instrument: Security) extends Actor
  with ActorLogging {

  val auctionMechanism: ActorRef = context.actorOf(Props[A], "auction-mechanism")

  val clearingMechanism: ActorRef = context.actorOf(Props[C], "clearing-mechanism")

  def receive: Receive = {
    case order: OrderLike => auctionMechanism forward order
    case fill: FillLike => clearingMechanism forward fill
  }

}

      

Instances of this class can be created as follows:

val stockMarket = SecuritiesMarket[DoubleAuctionMechanism, CCPClearingMechanism](Security("GOOG"))
val derivativesMarket = SecuritiesMarket[BatchAuctionMechanism, BilateralClearingMechanism](Security("SomeDerivative"))

      

There are many possible combinations of auction mechanism types and cleanup engine types that I could use when instantiating SecuritiesMarket

for a particular model / simulation.

Is it possible to specify the type parameters that I want to use in a given simulation in a file application.conf

?

+3


source to share


2 answers


I see two questions here.

Is it possible to get an instance Class

from a string?

Yes.

val cls: Class[DoubleAuctionMechanism] = Class.forName("your.app.DoubleAuctionMechanism").asInstanceOf[Class[DoubleAuctionMechanism]]

      

You will still need the listing as it forName

returns Class[_]

.

Can I instantiate a type with type parameters not known at compile time?

Good, but not good.

object SecuritiesMarket {
    def apply[A, C](clsAuc: Class[A], clsClr: Class[C])(security: Security): SecuritiesMarket[A, C] = {
        SecuritiesMarket[A, C](security)
    }
}

      




I think auction mechanisms and cleaning mechanisms depend on SecurityMarket

. I am assuming that you create them somehow in your constructor (how?). If that's the case, why not just pass them as a constructor parameter?

Edit:

I can't see how to create child actors inside SecurityMarket

Answering this in the comments; Props[T]

can also be written as Props[T](classOfT)

, which can be simplified as Props(classOfT)

. These three are the same. So the following code:

val auctionMechanism: ActorRef = context.actorOf(Props[A], "auction-mechanism")

      

Can be replaced with:

val classOfA = Class.forName("path.to.A")
val auctionMechanism: ActorRef = context.actorOf(Props(classOfA), "auction-mechanism")

      

+2


source


First, application.conf is a runtime artifact, and its contents, as far as I know, have not usually been parsed at compile time until now. When the file is parsed at runtime, the parser instantiates the Config class, which then manages the Akka installation.

The Configafe Configafe library project script is pretty good and the linked documentation has all the details:

https://github.com/typesafehub/config/blob/master/README.md .



Second, since template parameters are not available at runtime due to type erasure, you generally cannot use application.conf to manage templates. You can create a custom build step to parse the application.conf and change the code before compiling, but that might not be what you want. (And if you want a custom build step, a different .conf might be appropriate.)

Instead, you can try to simply exclude the type parameters for the securities market. Then create a single, simple implementation of the auction and clearing members. Implement these members by reading the names of the appropriate mechanisms from application.conf, instantly creating a customized mechanism, and delegating the created mechanism. Mechanism classes can be independent of Akka, which might be nice if you keep most of your logic?

0


source