<?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>spiros.blog() &#187; Scala</title>
	<atom:link href="http://www.tzavellas.com/techblog/category/scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tzavellas.com/techblog</link>
	<description>Spiros Tzavellas’s blog, mostly on software development and Java.</description>
	<lastBuildDate>Sun, 22 Aug 2010 20:25:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Making Guice more Scala friendly</title>
		<link>http://www.tzavellas.com/techblog/2010/08/22/making-guice-more-scala-friendly/</link>
		<comments>http://www.tzavellas.com/techblog/2010/08/22/making-guice-more-scala-friendly/#comments</comments>
		<pubDate>Sun, 22 Aug 2010 20:25:28 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[Guice]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=265</guid>
		<description><![CDATA[Guice might be one of the few libraries that is easier to use in Java than it is in Scala. The main reason for this is the absence of class literals in Scala. Instead of writing Service.class in Scala we write classOf[Service] which is longer and less readable. To illustrate this lets compare two Guice [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/google-guice/">Guice</a> might be one of the few libraries that is easier to use in <a href="http://www.oracle.com/technetwork/java/index.html">Java</a> than it is in <a href="http://www.scala-lang.org">Scala</a>. The main reason for this is the absence of class literals in Scala. Instead of writing <code>Service.class</code> in Scala we write <code>classOf[Service]</code> which is longer and less readable. To illustrate this lets compare two Guice modules, one defined in Java and the other in Scala.</p>
<p>Below we have a small Guice module (copied from the documentation of <a href="http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/AbstractModule.html"><code>AbstractModule</code></a>) defined in Java.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyModule <span style="color: #000000; font-weight: bold;">extends</span> AbstractModule <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    bind<span style="color: #009900;">&#40;</span>Service.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span>ServiceImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">in</span><span style="color: #009900;">&#40;</span>Singleton.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    bind<span style="color: #009900;">&#40;</span>CreditCardPaymentService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    bind<span style="color: #009900;">&#40;</span>PaymentService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span>CreditCardPaymentService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    bindConstant<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">annotatedWith</span><span style="color: #009900;">&#40;</span>Names.<span style="color: #006633;">named</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;port&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8080</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In Scala the above code would be:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> MyModule <span style="color: #0000ff; font-weight: bold;">extends</span> AbstractModule <span style="color: #F78811;">&#123;</span>
   <span style="color: #0000ff; font-weight: bold;">protected</span> <span style="color: #0000ff; font-weight: bold;">def</span> configure<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
     bind<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>Service<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">to</span><span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>ServiceImpl<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">in</span><span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>Singleton<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>
     bind<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>CreditCardPaymentService<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>
     bind<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>PaymentService<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">to</span><span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>CreditCardPaymentService<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>
     bindConstant<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">annotatedWith</span><span style="color: #F78811;">&#40;</span>Names.<span style="color: #000000;">named</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;port&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">to</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">8080</span><span style="color: #F78811;">&#41;</span>
   <span style="color: #F78811;">&#125;</span>
 <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>As you can see the code is longer and it looses the DSL feel that it has in the Java version. This fact may lead Scala developers away from Guice since defining Guice modules in Scala feels awkward and leaves a lot to be desired.</p>
<p>For this reason I&#8217;ve created a small project, called <a href="http://github.com/sptz45/sse-guice">sse-guice</a>, that extends Guice&#8217;s internal DSL with methods that make defining Guice modules in Scala more pleasant.</p>
<p>What I&#8217;ve done is to extend the binder interfaces of Guice (defined in <a href="http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/binder/package-summary.html"><code>com.google.inject.binder</code></a> package) and add a method that takes a <code>Manifest</code> for each method that takes a <code>Class</code> as a parameter. Furthermore I&#8217;ve extended the <a href="http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/AbstractModule.html"><code>AbstractModule</code></a> class (the entry point of the Guice internal DSL) and added a bind method that accepts a <code>Manifest</code> and I&#8217;ve also overridden all the methods that return a binder interface to return the extended binder interfaces provided by <a href="http://github.com/sptz45/sse-guice">sse-guice</a>.</p>
<p>By utilizing <a href="http://github.com/sptz45/sse-guice">sse-guice</a> the above module can be rewritten as:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> MyModule <span style="color: #0000ff; font-weight: bold;">extends</span> ScalaModule <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">protected</span> <span style="color: #0000ff; font-weight: bold;">def</span> configure<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    bind<span style="color: #F78811;">&#91;</span>Service<span style="color: #F78811;">&#93;</span>.<span style="color: #000000;">to</span><span style="color: #F78811;">&#91;</span>ServiceImpl<span style="color: #F78811;">&#93;</span>.<span style="color: #000000;">in</span><span style="color: #F78811;">&#91;</span>Singleton<span style="color: #F78811;">&#93;</span>
    bind<span style="color: #F78811;">&#91;</span>CreditCardPaymentService<span style="color: #F78811;">&#93;</span>
    bind<span style="color: #F78811;">&#91;</span>PaymentService<span style="color: #F78811;">&#93;</span>.<span style="color: #000000;">to</span><span style="color: #F78811;">&#91;</span>CreditCardPaymentService<span style="color: #F78811;">&#93;</span>
    bindConstant<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">annotatedWithName</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;port&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">to</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">8080</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>As you can see the code now is shorter, easier to read and feels like a proper internal DSL.</p>
<p>Another benefit of using <code>Manifest</code> as a parameter is that we can now avoid creating <code>TypeLiteral</code>s when we bind generic types. To bind a generic type in Guice you have to write:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">bind<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> TypeLiteral<span style="color: #F78811;">&#91;</span>Validator<span style="color: #F78811;">&#91;</span>Registration<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span><span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">to</span><span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>RegistrationVSpec<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>but when using <a href="http://github.com/sptz45/sse-guice">sse-guice</a> we can simply write:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">bind<span style="color: #F78811;">&#91;</span>Validator<span style="color: #F78811;">&#91;</span>Registration<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span>.<span style="color: #000000;">to</span><span style="color: #F78811;">&#91;</span>RegistrationVSpec<span style="color: #F78811;">&#93;</span></pre></div></div>

<p>because <code>Manifest</code> also holds any type arguments and <a href="http://github.com/sptz45/sse-guice">sse-guice</a> automatically creates a <code>TypeLiteral</code> when the <code>Manifest</code> contains type arguments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2010/08/22/making-guice-more-scala-friendly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using self types for trait composition</title>
		<link>http://www.tzavellas.com/techblog/2009/12/07/using-self-types-for-trait-composition/</link>
		<comments>http://www.tzavellas.com/techblog/2009/12/07/using-self-types-for-trait-composition/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 15:01:37 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[trait]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=227</guid>
		<description><![CDATA[While using traits in a small Scala project, I finally understood when to use self types. Self types are often characterized by Scala newbies as an incomprehensible language feature with no obvious usage. After coding with traits for a while I understood that self types are an essential information hiding tool for composing traits. I [...]]]></description>
			<content:encoded><![CDATA[<p>While using traits in a small Scala project, I finally understood when to use self types. Self types are often characterized by Scala newbies as an <a href="http://www.naildrivin5.com/scalatour/wiki_pages/ExplcitlyTypedSelfReferences">incomprehensible language feature</a> with no obvious usage. After coding with traits for a while I understood that self types are an essential information hiding tool for composing traits.</p>
<p>I am coding a small JMX library in Scala, similar to <a href="http://static.springsource.org/spring/docs/2.5.x/reference/jmx.html">Spring&#8217;s JMX</a> module and to the <a href="http://github.com/martint/jmxutils">jmxutils</a> library. In my project I have a <code>MBeanInfoAssembler</code> trait, that is a strategy interface, whose role is to create a <code>ModelMBeanInfo</code> from a <code>Class</code>. Implementations of that trait will use reflection to discover the attributes and operations that will be used to export instances of the <code>Class</code> to JMX.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> MBeanInfoAssembler <span style="color: #F78811;">&#123;</span>  
  <span style="color: #0000ff; font-weight: bold;">def</span> createMBeanInfo<span style="color: #F78811;">&#40;</span>clazz<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> ModelMBeanInfo
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The first implementation of the trait was the <code>SimpleMBeanInfoAssembler</code> class which uses reflection and some conventions to discover the attributes and operations.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> SimpleMBeanInfoAssembler <span style="color: #0000ff; font-weight: bold;">extends</span> MBeanInfoAssembler <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> createMBeanInfo<span style="color: #F78811;">&#40;</span>clazz<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> ModelMBeanInfo <span style="color: #000080;">=</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> ModelMBeanInfoSupport<span style="color: #F78811;">&#40;</span>clazz.<span style="color: #000000;">getName</span>,
                              clazz.<span style="color: #000000;">getSimpleName</span>,
                              attributes<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>, 
                              operations<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">def</span> attributes<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanAttributeInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the attributes from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">def</span> operations<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanOperationInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the operations from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>In the above code I have removed the implementations of the <code>attributes</code> and <code>operations</code> methods because they are not important for this example. What is important is the <code>createMBeanInfo</code> method which just calls the <code>ModelMBeanInfoSupport</code> constructor with the results of the <code>attributes</code> and <code>operations</code> methods. If we leave the code like this, when we try to provide a second implementation of the <code>MBeanInfoAssembler</code> trait we will probably copy-paste that method&#8217;s code.</p>
<p>In Scala, traits can have concrete methods so our first though is to move the implementation of the <code>createMBeanInfo</code> method to the <code>MBeanInfoAssembler</code> trait. After doing that our trait becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> MBeanInfoAssembler <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> createMBeanInfo<span style="color: #F78811;">&#40;</span>clazz<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> ModelMBeanInfo <span style="color: #000080;">=</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> ModelMBeanInfoSupport<span style="color: #F78811;">&#40;</span>clazz.<span style="color: #000000;">getName</span>,
                              clazz.<span style="color: #000000;">getSimpleName</span>,
                              attributes<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>, 
                              operations<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> attributes<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanAttributeInfo<span style="color: #F78811;">&#93;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> operations<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanOperationInfo<span style="color: #F78811;">&#93;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>and the implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> SimpleMBeanInfoAssembler <span style="color: #0000ff; font-weight: bold;">extends</span> MBeanInfoAssembler <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> attributes<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanAttributeInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the attributes from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> operations<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanOperationInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the operations from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>This refactoring has the obvious advantage of removing the repetition that would be needed to create multiple implementations of <code>MBeanInfoAssembler</code> but also one major disadvantage over our previous code.</p>
<p>In order to support the implementation of the <code>createMBeanInfo</code> method the <code>MBeanInfoAssembler</code> trait gets updated with two new <em>public abstract methods</em> (<code>attributes</code> and <code>operations</code>). Those methods are useless to the clients of the trait, who only want to create a <code>ModelMBeanInfo</code> from a <code>Class</code>, and are there only to support the trait&#8217;s implementation. To correct this defect in our design we need to do another refactoring by introducing a new trait and connecting the two traits together using a self type annotation.</p>
<p>If you are new to Scala you might wonder what is a self type? In the Scala documentation self types are described as a way to <em>&#8220;declare the type of the value <code>this</code> explicitly&#8221;</em> which might be accurate but sounds at least confusing for a newbie like me.</p>
<p>In a well designed object-oriented system we achive reuse and flexibility when our system has lots of small objects that have a single responsibility and are explicitely connected using interfaces that represent the role of the connection. Usually those connections are made using constructor arguments. The power of such system comes from the testability and reusability of the small objects and the flexibility of composing those objects to solve a particular problem.</p>
<p>Since traits in Scala can have code (method implementations), if we want to create an equally well designed and flexible system using traits we need to be able to do the same. The problem is that traits do not have constructors so we need a new construct to declare trait dependencies and also a way to compose traits together. The solution to the first problem are the self type annotations and the solution to the second is multiple inheritance.</p>
<p>In the previous example the <code>MBeanInfoAssembler</code> trait had many responsibilities assigned to it. It must be able to create a <code>ModelMBeanInfo</code> given a <code>Class</code> and it must also extract any metadata in order to find the right attributes and operations for the given <code>Class</code>. We decide to move the second responsibility to a new trait called <code>MBeanMetadataExtractor</code> that has the <code>attributes</code> and <code>operations</code> methods.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> MBeanMetadataExtractor <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> attributes<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanAttributeInfo<span style="color: #F78811;">&#93;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> operations<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanOperationInfo<span style="color: #F78811;">&#93;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Now we need to update the <code>MBeanInfoAssembler</code> trait and to somehow connect it to the <code>MBeanMetadataExtractor</code> trait so it can use the <code>attributes</code> and <code>operations</code> methods. To do this we will use a self type annotation.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> MBeanInfoAssembler <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #000080;">:</span> MBeanMetadataExtractor <span style="color: #000080;">=&gt;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> createMBeanInfo<span style="color: #F78811;">&#40;</span>clazz<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> ModelMBeanInfo <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> ModelMBeanInfoSupport<span style="color: #F78811;">&#40;</span>clazz.<span style="color: #000000;">getName</span>,
                              clazz.<span style="color: #000000;">getSimpleName</span>,
                              attributes<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>,
                              operations<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The syntax of the self type annotation is <code>this: MBeanMetadataExtractor =></code> and it tells the compiler that a <code>MBeanInfoAssembler</code> can be implemented in a class only if that class also implements the <code>MBeanMetadataExtractor</code> trait. Using this assumption the compiler makes the methods of the <code>MBeanMetadataExtractor</code> available for use in the <code>MBeanInfoAssembler</code> (in our case the <code>attributes</code> and <code>operations</code> methods).</p>
<p>The class <code>SimpleMBeanInfoAssembler</code> now has to implement both <code>MBeanInfoAssembler</code> and <code>MBeanMetadataExtractor</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> SimpleMBeanInfoAssembler <span style="color: #0000ff; font-weight: bold;">extends</span> MBeanInfoAssembler <span style="color: #0000ff; font-weight: bold;">with</span> MBeanMetadataExtractor <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> attributes<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanAttributeInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the attributes from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> operations<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanOperationInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the operations from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>By doing the above changes we were able to hide the <code>attributes</code> and <code>operations</code> methods from the clients of the <code>MBeanInfoAssembler</code> trait. Obviously those methods exist in the public interface of <code>SimpleMBeanInfoAssembler</code> but in a well designed system other classes will connect with <code>SimpleMBeanInfoAssembler</code> by using one of its traits (interfaces) so this is not a problem.</p>
<p>In conclusion, if we want to move method implementations inside traits then we risk polluting the interface of those traits with abstract methods that support the implementation of the concrete methods and are unrelated with the main responsibility of the trait. A solution to this problem is to move those abstract methods in other traits and compose the traits together using self type annotations and multiple inheritance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2009/12/07/using-self-types-for-trait-composition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
