Suppose I need to implement a custom message-oriented protocol in Scala. I need to implement also the client/server code.
I would define "cases classes" for protocol messages as follows:
trait Message case class Request1(...) extends Message case class Response1(...) extends Message case class Request2(...) extends Message case class Response2(...) extends Message ... // other requests/responses
Now I need functions to read/write the messages from/to input/output streams and handle the messages.
def read(in: InputStream): Message = {...} def write(msg: Message, out: OutputStream) {...} def handle(msg:Message): Message = msg match { case req: Request1 = ... // handle Request1 case resp: Response1 = ... // handle Response1 ... // cases for all other message types }
I guess it works but I wonder if I can improve the solution. How would you correct or improve it ?