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

Clever Tools: a year of enhancements for your deployments, on the road to v4

A command line interface (CLI) is at the core of developer experience. At Clever Cloud, we have been providing Clever Tools for almost 10 years.
Engineering Features

Otoroshi with LLM: simplify your API and AI service management on Clever Cloud

Your applications and services are evolving in an increasingly complex environment, requiring effective management of APIs and interactions with artificial intelligence models such as the very popular LLMs (Large Language Models).
Features

Markitdown-as-a-Service: from AI to production on Clever Cloud

Every day, new tools are released, AI brings new perspectives, you have new ideas. It's one of Clever Cloud's missions to help you to develop and test them in real-life conditions, effortlessly, before making them available to everyone.
Engineering