I am using the akka library and supplying a partial function to be implemented by an actor at runtime via a hot swap.
The akka hot swap takes an argument in the form PartialFunction[Any, Unit]. I have defined mine as the following:
class Fake1Reader extends AbstractReader {
def read: PartialFunction[Any, Unit] = {
case readingRequest: ReadingRequest => {
var reading: Reading = new ReadingImpl(readingRequest.getResourceId, "payload",
Calendar.getInstance.getTime,
readingRequest.getfrequency, readingRequest.getMappingName,
readingRequest.getClassificationType,
readingRequest.getReadingRequestId)
sendConsumeMessage(reading)
}
}
}
so in order to use this function I have to supply a new Fake1Reader().read.
Is there any more concise way of doing this class using apply or extending Function or PartialFunction?
If your
AbstractReaderis stateless, you can defineobjectinstead ofclassto avoid unnecessary object creation on every usage,and place your functions there as immutableval's.Also, companion object
akka.actor.Actordefines typeReceiveas an alias forPartialFunction[Any, Unit], so you can write your partial functions like this:usage: