<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rodolphe Belouin, Author at Clever Cloud</title>
	<atom:link href="https://stagingv6.cleverapps.io/blog/author/belouin/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>From Code to Product</description>
	<lastBuildDate>Fri, 27 Oct 2023 10:25:15 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://staging-cc-assetsv6.cellar-c2.services.clever-cloud.com/uploads/2023/03/cropped-cropped-favicon-32x32.png</url>
	<title>Rodolphe Belouin, Author at Clever Cloud</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Traits extended by one-line case classes</title>
		<link>https://stagingv6.cleverapps.io/blog/engineering/2012/06/21/traits-extended-by-one-line-case-classes/</link>
		
		<dc:creator><![CDATA[Rodolphe Belouin]]></dc:creator>
		<pubDate>Thu, 21 Jun 2012 00:00:00 +0000</pubDate>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Developers]]></category>
		<guid isPermaLink="false">https://www2.cleverapps.io/wp/blog/technology/2012/06/21/traits-extended-by-one-line-case-classes/</guid>

					<description><![CDATA[Let&#39;s start talking about Scala, that pretty language making you able to use both object-oriented and functional programming paradigms.If you&#39;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 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Let&#39;s start talking about Scala, that pretty language making you able to use both object-oriented and functional programming paradigms.<br />If you&#39;ve never heard about it, take a look at <a href="http://twitter.github.com/scala_school/">Scala School</a>. You have to be at ease with trait and case class concepts to understand this post.</p>
<p><span id="more-2781"></span></p>
<p>We all agree: keeping an abstract layer in your code is something good, but it&#39;s often painful when you have to rewrite n-times some children having a similar implementation.<br />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.</p>
<h2 id="trivial-trait">Trivial trait</h2>
<p>Let&#39;s define a trait that will be the minimal representation of a message.</p>
<pre><code class="language-scala">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
</code></pre>
<p>No need for writing overriding methods. Actually, <em>id</em> and <em>content</em> parameters are defined as methods that implicitly override Message&#39;s methods. Nice, isn&#39;t it?</p>
<h2 id="trait-using-serialization">Trait using serialization</h2>
<p>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.</p>
<pre><code class="language-scala">trait MessageSerializer[M &lt;: 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 &lt;- JsonParser.parseOpt(s)
      m &lt;- jvalue.extractOpt[M]
    } yield m
  }
}

object DefaultMessageSerializer extends MessageSerializer[DefaultMessage]
object FromToMessageSerializer extends MessageSerializer[FromToMessage]
</code></pre>
<p>If you don&#39;t know what are apply and unapply methods, just take a look at this sample code to understand their usage:</p>
<pre><code class="language-scala">/* implicit call to apply method */
val a = DefaultMessage(&quot;1234&quot;, &quot;Hello world!&quot;)
println(DefaultMessageSerializer(a)) // print: {&quot;id&quot;: &quot;1234&quot;, &quot;content&quot;: &quot;Hello world!&quot;}

/* implicit call to unapply method */
&quot;&quot;&quot;{&quot;id&quot;: &quot;5678&quot;, &quot;content&quot;: &quot;Love&quot;, &quot;from&quot;: &quot;me&quot;, &quot;to&quot;: &quot;you&quot;}&quot;&quot;&quot; match {
  case FromToMessageSerializer(m) =&gt; println(m) // print FromToMessage(&quot;5678&quot;, &quot;Love&quot;, &quot;me&quot;, &quot;you&quot;)
  case _ =&gt; println(&quot;error&quot;)
}
</code></pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
