Traits extended by one-line case classes

Let's start talking about Scala, that pretty language making you able to use both object-oriented and functional programming paradigms.
If you've never heard about it, take a look at Scala School. You have to be at ease with trait and case class concepts to understand this post.

We all agree: keeping an abstract layer in your code is something good, but it's often painful when you have to rewrite n-times some children having a similar implementation.
I always try to keep that children readable and fast understandable by making them one-liners. Adding a child becomes quite easy, even for a non-scala developer.

Trivial trait

Let's define a trait that will be the minimal representation of a message.

trait Message {
  def id: String
  def content: String

case class DefaultMessage(id: String, content: String) extends Message
case class FromToMessage(id: String, content: String, from: String, to: String) extends Message

No need for writing overriding methods. Actually, id and content parameters are defined as methods that implicitly override Message's methods. Nice, isn't it?

Trait using serialization

OK, that was quite simple. But how will you do if you have to create custom serializer object for each message type? In that example, we will be using the lift framework to (de)serialize JSON.

trait MessageSerializer[M <: Message] {
  implicit val format = DefaultFormats

  def apply(m: M): String = {

  def unapply(s: String)(implicit mf: Manifest[M]): Option[M] = {
    for {
      jvalue <- JsonParser.parseOpt(s)
      m <- jvalue.extractOpt[M]
    } yield m

object DefaultMessageSerializer extends MessageSerializer[DefaultMessage]
object FromToMessageSerializer extends MessageSerializer[FromToMessage]

If you don't know what are apply and unapply methods, just take a look at this sample code to understand their usage:

/* implicit call to apply method */
val a = DefaultMessage("1234", "Hello world!")
println(DefaultMessageSerializer(a)) // print: {"id": "1234", "content": "Hello world!"}

/* implicit call to unapply method */
"""{"id": "5678", "content": "Love", "from": "me", "to": "you"}""" match {
  case FromToMessageSerializer(m) => println(m) // print FromToMessage("5678", "Love", "me", "you")
  case _ => println("error")


À lire également

Clever Cloud joins the Eclipse Foundation: a commitment to the future of European open source

Clever Cloud, a French provider of Platform as a Service (PaaS) hosting and deployment solutions, is proud to become a contributing member of the Eclipse Foundation, a leading not-for-profit organisation in the field of open source.

Up to €100,000 in funding to adopt Hyper Open X technologies

The Hyper Open X consortium, made up of sixteen major French cloud players, has launched anambitious call for projects designed to accelerate the adoption of open source technologies for cloud and edge computing.

Clever Cloud announces international growth strategy and the expansion of leadership team

Nantes, France - Clever Cloud, French creator of Platform as a Service (PaaS) hosting and deployment solutions, announced today its intention to expand internationally, with a particular focus on the African and South Asian markets.
Company Press