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 = {
    Serialization.write(m)
  }

  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")
}

Blog

À lire également

Azimutt on Clever Cloud: easily view and manage your SQL databases

Simply creating databases is great, but being able to explore, analyze and manage them in a user-friendly interface is even better. With this in mind, we worked with Loïc Knuchel to integrate Azimutt on Clever Cloud.
Company

FrankenPHP, sessions in Materia KV, lifecycle: what will change in PHP on Clever Cloud

Since the first week of the year, we've delivered. There have been many new features on Clever Cloud, some more visible than others.
Company

SuperBOL: The COBOL revolution in the Cloud

COBOL, a programming language that is over 60 years old, continues to power a large proportion of the IT systems of the world's major companies, particularly in the financial and insurance sectors.
Features