<?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>Blog of Adam Warski</title>
	<atom:link href="http://www.warski.org/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.warski.org/blog</link>
	<description>Java, Scala, programming, ...</description>
	<lastBuildDate>Sun, 28 Apr 2013 12:48:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>MacWire 0.2: Scopes are simple!</title>
		<link>http://www.warski.org/blog/2013/04/macwire-0-2-scopes-are-simple/</link>
		<comments>http://www.warski.org/blog/2013/04/macwire-0-2-scopes-are-simple/#comments</comments>
		<pubDate>Thu, 25 Apr 2013 18:14:51 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[Macros]]></category>
		<category><![CDATA[MacWire]]></category>
		<category><![CDATA[Modularity]]></category>
		<category><![CDATA[Object oriented]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Weld]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=970</guid>
		<description><![CDATA[MacWire generates new instance creation code of given classes, using values in the enclosing type for constructor parameters, with the help of Scala Macros. DI container replacement. Version 0.2 has &#8230; <a class="readmore" href="http://www.warski.org/blog/2013/04/macwire-0-2-scopes-are-simple/">Continue Reading &#8594;</a>]]></description>
				<content:encoded><![CDATA[<p><em><a href="https://github.com/adamw/macwire">MacWire</a> generates new instance creation code of given classes, using values in the enclosing type for constructor parameters, with the help of Scala Macros. DI container replacement.</em></p>
<p>Version 0.2 has just landed!</p>
<h3>First things first &#8230;</h3>
<p>First, some bad news. Due to limitations of the current macros implementation in Scala (for more details see <a href="https://groups.google.com/forum/?fromgroups=#!topic/scala-user/k_2KCvO5g04">this discussion</a>) in order to avoid occasional compilation errors it is necessary to add type ascriptions to the dependencies. This is a way of helping the type-checker that is invoked by the macro to figure out the types of the values which can be wired.</p>
<p>In practice it is good to always write the type ascription. For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> MyModule <span style="color: #F78811;">&#123;</span>
   ...
   <span style="color: #000000;">lazy</span> <span style="color: #0000ff; font-weight: bold;">val</span> theUserFinder<span style="color: #000080;">:</span> UserFinder <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>UserFinder<span style="color: #F78811;">&#93;</span>
   ...
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>This is a major inconvenience, but hopefully will get resolved some day. See the &#8220;Limitations&#8221; section in the <a href="https://github.com/adamw/macwire">README</a> for more details.</p>
<p><strong>Update 28/04/2013</strong> As noted in the comments below, the recommendation for adding type ascriptions may be in fact relaxed by adding special-handling for declarations that are simple <code>wire[X]</code> expressions. So stay tuned for future releases for improvements :)</p>
<p>Also, it is worth noting that the type ascription may be a subtype of the wired type. This can be useful if you want to expose e.g. a trait that the wired class extends, instead of the full implementation.</p>
<h3>&#8230; to the point</h3>
<p>Now, to the good news. Especially when developing web applications, it is very useful to scope some of the dependencies, so that there&#8217;s a new instance for each <strong>request</strong> (but for each dependency usage, it is the same instance in one request), or that there&#8217;s a single instance per <strong>http session</strong>.</p>
<p>So far MacWire had two &#8220;built-in&#8221; scopes: <strong>singleton</strong> (declare the dependency as <code>lazy val</code>) and <strong>dependent</strong> (<code>def</code>). Now it is possible to create custom scopes, by implementing the <code>Scope</code> trait.</p>
<p>Scopes often seem &#8220;magical&#8221;; in traditional frameworks it is usually necessary to add an annotation to the class and then the dependency is automagically turned into a scoped one by the container. </p>
<p>In fact implementing a scoped bean is quite simple. We need two things. Firstly, instead of using instances directly, we need to use a proxy which delegates method calls to the &#8220;current instance&#8221;. What &#8220;current instance&#8221; means is scope-specific.</p>
<p>Secondly, we need a way to read a value from the scope, and store a value in the scope, if there&#8217;s no value yet. The two methods in the <code>Scope</code> trait correspond directly to the two cases outlined above.</p>
<p>Using a scope is quite straightforward; the scope of a dependency is declared in the wiring code, in the module. For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> WebModule <span style="color: #F78811;">&#123;</span>
   lazy <span style="color: #0000ff; font-weight: bold;">val</span> loggedInUser<span style="color: #000080;">:</span> LoggedInUser <span style="color: #000080;">=</span> session<span style="color: #F78811;">&#40;</span>wire<span style="color: #F78811;">&#91;</span>LoggedInUser<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
   <span style="color: #0000ff; font-weight: bold;">def</span> session<span style="color: #000080;">:</span> Scope
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Note that the scope here is abstract (only the name suggests that it is a session scope). This has a couple of advantages. For testing, we can substitute a <code>NoOpScope</code>. Also, we can move the framework-integration code to the integration layer, or even provide a couple of implementations depending on the framework/container used.</p>
<p>MacWire comes with a <code>Scope</code> implementation targeted at synchronous web frameworks, <code>ThreadLocalScope</code>. The scope needs to be associated and disassociated with a scope storage. To implement a:</p>
<ul>
<li><em>request scope</em>, we need a new empty storage for every request (<code>associateWithEmptyStorage</code> method)</li>
<li><em>session scope</em>, the storage (a <code>Map</code>) should be stored in the <code>HttpSession</code> (<code>associate(Map)</code> method)</li>
</ul>
<p>This association can be done, for example, in a servlet filter:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> ScopeFilter<span style="color: #F78811;">&#40;</span>sessionScope<span style="color: #000080;">:</span> ThreadLocalScope<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Filter <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> init<span style="color: #F78811;">&#40;</span>filterConfig<span style="color: #000080;">:</span> FilterConfig<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span><span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> doFilter<span style="color: #F78811;">&#40;</span>request<span style="color: #000080;">:</span> ServletRequest, response<span style="color: #000080;">:</span> ServletResponse, chain<span style="color: #000080;">:</span> FilterChain<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> httpRequest <span style="color: #000080;">=</span> request.<span style="color: #000000;">asInstanceOf</span><span style="color: #F78811;">&#91;</span>HttpServletRequest<span style="color: #F78811;">&#93;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> sessionScopeStorage <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> ScopeStorage <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">def</span> get<span style="color: #F78811;">&#40;</span>key<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> Option<span style="color: #F78811;">&#40;</span>httpRequest.<span style="color: #000000;">getSession</span>.<span style="color: #000000;">getAttribute</span><span style="color: #F78811;">&#40;</span>key<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">def</span> set<span style="color: #F78811;">&#40;</span>key<span style="color: #000080;">:</span> String, value<span style="color: #000080;">:</span> Any<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        httpRequest.<span style="color: #000000;">getSession</span>.<span style="color: #000000;">setAttribute</span><span style="color: #F78811;">&#40;</span>key, value<span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    sessionScope.<span style="color: #000000;">withStorage</span><span style="color: #F78811;">&#40;</span>sessionScopeStorage<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      chain.<span style="color: #000000;">doFilter</span><span style="color: #F78811;">&#40;</span>request, response<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> destroy<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span><span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Note also that it is trivial to use a scoped value e.g. in an asynchronous process, where no web request is available: simply associate the scope with any storage, can be a temporary map. In traditional web frameworks this usually requires some amount of hacking.</p>
<p>To sum up, to use a scope in MacWire you need to:</p>
<ul>
<li>declare a <code>Scope</code> in your module, use if for scoped dependencies</li>
<li>provide an implementation of the scope, e.g. <code>ThreadLocalScope</code></li>
<li>associate the scope with a storage, e.g. in a servlet filter</li>
</ul>
<p>You can see how this works in practice by browsing &#038; running a <a href="http://www.scalatra.org/">Scalatra</a>+MacWire example application. To run, clone MacWire and use this command: <code>sbt examples-scalatra/run</code>. To browse the code, simply head to <a href="https://github.com/adamw/macwire/tree/master/examples/scalatra/src/main/scala/com/softwaremill/macwire/examples/scalatra">GitHub</a>.</p>
<p><a href="http://www.warski.org/blog/wp-content/uploads/2013/04/2013-04-25_1649.png"><img src="http://www.warski.org/blog/wp-content/uploads/2013/04/2013-04-25_1649-300x105.png" alt="2013-04-25_1649" width="300" height="105" class="aligncenter size-medium wp-image-972" /></a></p>
<p>The example contains a request-scoped dependency (<code>SubmittedData</code>), session-scoped dependency (<code>LoggedInUser</code>) and three main services (<code>Service1, Service2, Service3</code>). Each class has some dependencies (declared in the constructor), and is wired using <code>wire[]</code>. The code is organised in two modules: <code>logic</code> and <code>servlet</code>. The services have methods to return a string-status, containing the current request and session-scoped values, so that it is possible to verify that indeed the values are shared properly. The servlet module contains a servlet which shows the service status, plus the scopes implementations.</p>
<p>Note that the <code>scopes</code> subproject is completely independent, and can be used stand-alone with manual wiring or any other library/framework. Its only dependency is javassist.</p>
<p>Have fun!<br />
Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/2013/04/macwire-0-2-scopes-are-simple/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Per-commit e-mail GitHub notifications</title>
		<link>http://www.warski.org/blog/2013/04/per-commit-e-mail-github-notifications/</link>
		<comments>http://www.warski.org/blog/2013/04/per-commit-e-mail-github-notifications/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 14:34:59 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[Code review]]></category>
		<category><![CDATA[CodeBrag]]></category>
		<category><![CDATA[GitHub]]></category>
		<category><![CDATA[SoftwareMill]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=931</guid>
		<description><![CDATA[One thing that I miss in GitHub is the ability to get e-mail notifications on each push/commit. There is an option to set an e-mail notification address in the repository &#8230; <a class="readmore" href="http://www.warski.org/blog/2013/04/per-commit-e-mail-github-notifications/">Continue Reading &#8594;</a>]]></description>
				<content:encoded><![CDATA[<p>One thing that I miss in GitHub is the ability to get e-mail notifications on each push/commit. There is an option to set an e-mail notification address in the repository settings, but you can only specify a single, global address, and only the administrator can do it.</p>
<p>So while waiting for <a href="http://www.codebrag.com/">CodeBrag beta</a>, which will bring a much better code-review experience, here&#8217;s how I get an e-mail notification on each push to the repositories I&#8217;m interested in. </p>
<p>The general idea is to get a RSS feed, and then use a simple <a href="https://ifttt.com">IFTTT</a> recipe to receive an e-mail whenever there&#8217;s a new item containing the words &#8220;pushed to&#8221; in the feed. The recipe looks like this:</p>
<p><a href="http://www.warski.org/blog/wp-content/uploads/2013/03/2013-03-19_1616.png"><img src="http://www.warski.org/blog/wp-content/uploads/2013/03/2013-03-19_1616-300x109.png" alt="2013-03-19_1616" width="300" height="109" class="aligncenter size-medium wp-image-932" /></a></p>
<p>There are two ways of getting an RSS feed with all the pushes. Firstly, you can get an RSS feed which aggregates pushes to <strong>all of the repositories that you are watching</strong>. Simply go to <a href="https://github.com/">GitHub</a>, log in, and in the upper-right corner you should see a &#8220;News Feed&#8221; link. Copy that to IFTTT and you&#8217;re done!</p>
<p>Secondly, you can get a feed for a single branch of a repository. Go to the repository you wish to get notifications for, and click on the &#8220;commits tab&#8221; (you should get a page like <a href="https://github.com/adamw/veripacks/commits/master">this one</a>). Next to &#8220;Commit History&#8221;, there&#8217;s a feed symbol, which is a link to the branch&#8217;s feed.</p>
<p>Hope this will be useful!<br />
Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/2013/04/per-commit-e-mail-github-notifications/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MacWire 0.1: Framework-less Dependency Injection with Scala Macros</title>
		<link>http://www.warski.org/blog/2013/04/macwire-0-1-framework-less-dependency-injection-with-scala-macros/</link>
		<comments>http://www.warski.org/blog/2013/04/macwire-0-1-framework-less-dependency-injection-with-scala-macros/#comments</comments>
		<pubDate>Thu, 04 Apr 2013 11:07:53 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[Macros]]></category>
		<category><![CDATA[MacWire]]></category>
		<category><![CDATA[Modularity]]></category>
		<category><![CDATA[Object oriented]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=952</guid>
		<description><![CDATA[Using Dependency Injection is almost a standard when developing software. However, in many cases it may seem that using the pattern implicates using a DI container/framework. But is a framework &#8230; <a class="readmore" href="http://www.warski.org/blog/2013/04/macwire-0-1-framework-less-dependency-injection-with-scala-macros/">Continue Reading &#8594;</a>]]></description>
				<content:encoded><![CDATA[<p>Using <em>Dependency Injection</em> is almost a standard when developing software. However, in many cases it may seem that using the pattern implicates using a DI container/framework. But is a framework really needed? </p>
<p>To implement DI all you really need is to remove the <code>new</code>s from your code, and move the dependencies to the constructor. There must be of course some place where the objects are created; for that you need a top-level (&#8220;main&#8221; class), where all the wiring is done. That&#8217;s where DI containers really help: they remove the tedious task of passing the right parameters to the constructors. Usually that&#8217;s done at run-time using reflection.</p>
<p><a href="https://github.com/adamw/macwire">MacWire</a> takes a different approach. Basing on declarations specifying which classes should be instantiated, it generates the code needed to create a new class instance, with the correct parameters taken from the enclosing type. This is done at compile-time using a <a href="http://scalamacros.org/">Scala Macro</a>. The code is then type-checked by the Scala compiler, so the whole process is type-safe, and if a dependency is missing, you&#8217;ll know that immediately (unlike with traditional DI containers).</p>
<p>For example, given:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> DatabaseAccess<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> SecurityFilter<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> UserFinder<span style="color: #F78811;">&#40;</span>databaseAccess<span style="color: #000080;">:</span> DatabaseAccess, securityFilter<span style="color: #000080;">:</span> SecurityFilter<span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> UserStatusReader<span style="color: #F78811;">&#40;</span>userFinder<span style="color: #000080;">:</span> UserFinder<span style="color: #F78811;">&#41;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">trait</span> UserModule <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">import</span> com.<span style="color: #000000;">softwaremill</span>.<span style="color: #000000;">macwire</span>.<span style="color: #000000;">MacwireMacros</span>.<span style="color: #000080;">_</span>
&nbsp;
    lazy <span style="color: #0000ff; font-weight: bold;">val</span> theDatabaseAccess   <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>DatabaseAccess<span style="color: #F78811;">&#93;</span>
    lazy <span style="color: #0000ff; font-weight: bold;">val</span> theSecurityFilter   <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>SecurityFilter<span style="color: #F78811;">&#93;</span>
    lazy <span style="color: #0000ff; font-weight: bold;">val</span> theUserFinder       <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>UserFinder<span style="color: #F78811;">&#93;</span>
    lazy <span style="color: #0000ff; font-weight: bold;">val</span> theUserStatusReader <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>UserStatusReader<span style="color: #F78811;">&#93;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>The generated code will be:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> UserModule <span style="color: #F78811;">&#123;</span>
    lazy <span style="color: #0000ff; font-weight: bold;">val</span> theDatabaseAccess   <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> DatabaseAccess<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    lazy <span style="color: #0000ff; font-weight: bold;">val</span> theSecurityFilter   <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> SecurityFilter<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    lazy <span style="color: #0000ff; font-weight: bold;">val</span> theUserFinder       <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> UserFinder<span style="color: #F78811;">&#40;</span>theDatabaseAccess, theSecurityFilter<span style="color: #F78811;">&#41;</span>
    lazy <span style="color: #0000ff; font-weight: bold;">val</span> theUserStatusReader <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> UserStatusReader<span style="color: #F78811;">&#40;</span>theUserFinder<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>The classes that should be wired should be contained in a Scala <code>trait</code>, <code>class</code> or <code>object</code> (the container forms a &#8220;module&#8221;). MacWire looks up values from the enclosing type (trait/class/object), and from any super-traits/classes. Hence it is possible to combine several modules using inheritance.</p>
<p>Currently two scopes are supported; the dependency can be a singleton (declared as a <code>val</code>/<code>lazy val</code>) or a new instance can be created for each usage (declared as a <code>def</code>).</p>
<p>Note that this approach is very flexible; all that we are dealing with here is regular Scala code, so if a class needs to be created in some special way, there&#8217;s nothing stopping us from simply writing it down as code. Also, <code>wire[T]</code> can be nested inside a method&#8217;s body, and it will be expanded to new instance creation as well.</p>
<p>For integration testing a module, if for some classes we&#8217;d like to use a mock, a simple override suffices, e.g.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> UserModuleForTests <span style="color: #0000ff; font-weight: bold;">extends</span> UserModule <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">override</span> lazy <span style="color: #0000ff; font-weight: bold;">val</span> theDatabaseAccess <span style="color: #000080;">=</span> mockDatabaseAccess
    <span style="color: #0000ff; font-weight: bold;">override</span> lazy <span style="color: #0000ff; font-weight: bold;">val</span> theSecurityFilter <span style="color: #000080;">=</span> mockSecurityFilter
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>The project is a follow up of my <a href="http://www.warski.org/blog/2013/03/dependency-injection-with-scala-macros-auto-wiring/" title="Dependency injection with Scala macros: auto-wiring">earlier blog post</a>. There is also a similar project for Java, which uses annotation processors: <a href="https://github.com/square/dagger">Dagger</a>.</p>
<p>MacWire is available in <a href="http://repo1.maven.org/maven2/com/softwaremill/macwire/">Maven Central</a>. To use, simply add this line to your SBT build:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">libraryDependencies +<span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;com.softwaremill.macwire&quot;</span> <span style="color: #000080;">%%</span> <span style="color: #6666FF;">&quot;core&quot;</span> <span style="color: #000080;">%</span> <span style="color: #6666FF;">&quot;0.1&quot;</span></pre></td></tr></table></div>

<p>The <a href="https://github.com/adamw/macwire">code is on GitHub</a>, licensed under the Apache2 license, so feel free to use, fork &#038; explore. Take a look at the README which contains some more information.</p>
<p>Future plans include support for factories and by-name parameter lookup, support for configuration values and more scopes, like request or session scopes, which are very useful for web projects.</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/2013/04/macwire-0-1-framework-less-dependency-injection-with-scala-macros/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>How to replace a build module with Veripacks</title>
		<link>http://www.warski.org/blog/2013/03/how-to-replace-a-build-module-with-veripacks/</link>
		<comments>http://www.warski.org/blog/2013/03/how-to-replace-a-build-module-with-veripacks/#comments</comments>
		<pubDate>Tue, 26 Mar 2013 21:05:34 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Methodology]]></category>
		<category><![CDATA[Modularity]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[specification]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[verification]]></category>
		<category><![CDATA[veripacks]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=937</guid>
		<description><![CDATA[Compare the two trees below. In both cases the goal is to have an application with two independent modules (frontend and reporting), and one shared/common module (domain). The code in &#8230; <a class="readmore" href="http://www.warski.org/blog/2013/03/how-to-replace-a-build-module-with-veripacks/">Continue Reading &#8594;</a>]]></description>
				<content:encoded><![CDATA[<p>Compare the two trees below. In both cases the goal is to have an application with two independent modules (<code>frontend</code> and <code>reporting</code>), and one shared/common module (<code>domain</code>). The code in <code>frontend</code> shouldn&#8217;t be able to access code in <code>reporting</code>, and vice versa. Both modules can use the <code>domain</code> code. Ideally, we would like to check these access rules at build-time.</p>
<p><a href="http://www.warski.org/blog/wp-content/uploads/2013/03/2013-03-20_1910.png"><img src="http://www.warski.org/blog/wp-content/uploads/2013/03/2013-03-20_1910-300x297.png" alt="2013-03-20_1910" width="300" height="297" class="aligncenter size-medium wp-image-941" /></a></p>
<p>On the left, there&#8217;s a traditional solution using Maven build modules. Each build module has a pretty elaborate <code>pom.xml</code>, e.g.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;parent<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>parent<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.veripacks.battle<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/parent<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Veripacks vs Build Modules: Frontend<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>frontend<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.veripacks.battle<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>domain<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>On the right, on the other hand, we have a much simpler structure with only one build module. Each application module now corresponds to one top-level project package (see also <a href="http://blog.schauderhaft.de/2013/01/13/the-importance-of-packages/">this blog</a> on package naming conventions). </p>
<p>Notice the <code>package-info.java</code> files. There, using <a href="http://veripacks.org">Veripacks</a>, we can specify which packages are visible where. First of all, we specify that the code from top-level packages (<code>frontend</code>, <code>reporting</code> and <code>domain</code>) should be only accessible if explicitly imported, using <code>@RequiresImport</code>. Secondly, we specify that we want to access the <code>domain</code> package in <code>frontend</code> and <code>reporting</code> using <code>@Import</code>; e.g.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">@RequiresImport
@<span style="color: #000000; font-weight: bold;">Import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;org.veripacks.battle.domain&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.veripacks.battle.frontend</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.veripacks.Import</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.veripacks.RequiresImport</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, isn&#8217;t the Veripacks approach simpler? :) There is still build-time checking, which is possible by running a simple test (see the README for details). Plus, you can also use other Veripacks features, like <code>@Export</code> annotations, which is a generalized version of package-private scope, taking into account package hierarchies. There are also other benefits, like trivial sharing of test code (which is kind of hard with Maven), or much easier refactoring (introducing a new application module is a matter of adding a top-level package).</p>
<p>The immediate question that arises is &#8211; what about 3rd party libraries? Most probably, we&#8217;d like frontend-specific libraries to be accessible only in the <code>frontend</code> module, and reporting-specific ones in the <code>reporting</code> module. Well, not supported yet, but good news &#8211; that will be the scope of the next Veripacks release :).</p>
<p>You can view the example projects <a href="https://github.com/adamw/veripacks-vs-buildmodules">on GitHub</a>.</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/2013/03/how-to-replace-a-build-module-with-veripacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dependency injection with Scala macros: auto-wiring</title>
		<link>http://www.warski.org/blog/2013/03/dependency-injection-with-scala-macros-auto-wiring/</link>
		<comments>http://www.warski.org/blog/2013/03/dependency-injection-with-scala-macros-auto-wiring/#comments</comments>
		<pubDate>Thu, 14 Mar 2013 21:33:04 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Macros]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Weld]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=909</guid>
		<description><![CDATA[You can look at dependency injection as a fancy name for passing parameters to a function (or constructor arguments to a constructor). However usually, DI containers do much more than &#8230; <a class="readmore" href="http://www.warski.org/blog/2013/03/dependency-injection-with-scala-macros-auto-wiring/">Continue Reading &#8594;</a>]]></description>
				<content:encoded><![CDATA[<p>You can look at dependency injection as a fancy name for passing parameters to a function (or constructor arguments to a constructor). However usually, DI containers do much more than that. Among other things, one very nice feature is <strong>auto-wiring</strong>: instantiating the right objects with the right arguments. Most popular frameworks (<a href="http://www.springsource.org/">Spring</a>, <a href="https://code.google.com/p/google-guice/">Guice</a>, <a href="http://seamframework.org/Weld">CDI/Weld</a>) accomplish this task at runtime using reflection.</p>
<p>[rant]<br />
Doing the wiring at runtime with reflection has its downsides though. Firstly, there&#8217;s <strong>no compile-time checking</strong> that each dependency is satisfied. Secondly, we <strong>loose some of the flexibility</strong> we would have when doing things by hand, as we have to obey the rules by which the objects are created &#8220;automatically&#8221;. For example, if for some reason an object needs to be created manually, this requires a level of indirection (boilerplate), namely a factory. Finally, often the <strong>dependency injection is &#8220;global&#8221;,</strong> that is there is a single container with all the objects, it&#8217;s hard to create local/parametrized &#8220;universes&#8221; (Guice is an exception here). Finally-finally some frameworks do <strong>classpath scanning</strong>, which is slow, and sometimes can give unexpected results.<br />
[/rant]</p>
<p>Way too magical for such a simple thing. </p>
<p>But isn&#8217;t what we really want just a way to have all the <code>new</code>s with correct parameters generated for us? If you&#8217;re using Scala, and want code generation, the obvious answer are <a href="http://scalamacros.org/">macros</a>!</p>
<p>To finally show some code, given:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> A
<span style="color: #0000ff; font-weight: bold;">class</span> B
<span style="color: #0000ff; font-weight: bold;">class</span> C<span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">:</span> A, b<span style="color: #000080;">:</span> B<span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> D<span style="color: #F78811;">&#40;</span>b<span style="color: #000080;">:</span> B, c<span style="color: #000080;">:</span> C<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>it would be nice to have:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> a    <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> theB <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span> <span style="color: #008000; font-style: italic;">// &quot;theB&quot;, not &quot;b&quot;, just to show that we can use any name</span>
<span style="color: #0000ff; font-weight: bold;">val</span> theC <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>C<span style="color: #F78811;">&#93;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> d    <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>D<span style="color: #F78811;">&#93;</span></pre></td></tr></table></div>

<p>transformed to:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> a    <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> A<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> theB <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> B<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> theC <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> C<span style="color: #F78811;">&#40;</span>a, theB<span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> d    <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> D<span style="color: #F78811;">&#40;</span>theB, c<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>Turns out it&#8217;s possible, and even not very complicated.</p>
<p>A proof-of-concept is available on <a href="https://github.com/adamw/scala-macro-di">GitHub</a>. It&#8217;s very primitive and currently supports only one specific way of defining classes/wirings, but works :). If a dependency is missing, there&#8217;s a compile error. To check it out, simply clone the repo, run <code>sbt</code> and then invoke the task: <code>run-main com.softwaremill.di.DiExampleRunner</code> (<a href="https://github.com/adamw/scala-macro-di/blob/2cec0f6ad231ead205fcf47570c1150abbd496ac/examples/src/main/scala/com/softwaremill/di/DiExample.scala">implementation</a>). During compilation, you should see some info messages regarding the generated code, e.g.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>info<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>adamw<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>...<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>DiExample.scala:<span style="color: #000000;">13</span>: Generated code: new C<span style="color: #7a0874; font-weight: bold;">&#40;</span>a, theB<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>info<span style="color: #7a0874; font-weight: bold;">&#93;</span>   val c = wire<span style="color: #7a0874; font-weight: bold;">&#91;</span>C<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>info<span style="color: #7a0874; font-weight: bold;">&#93;</span>               ^</pre></td></tr></table></div>

<p>and then a proof that indeed the code was generated correctly: when the code is executed, the instances are printed to stdout so that you can see the arguments.</p>
<p>The macro here is of course the <code>wire</code> method (<a href="https://github.com/adamw/scala-macro-di/blob/2cec0f6ad231ead205fcf47570c1150abbd496ac/macros/src/main/scala/com/softwaremill/di/DiMacro.scala">implementation</a>). What it does is it first checks what are the parameters of the constructor of the class, and then for each parameter, tries to find a <code>val</code> defined in the enclosing class of the desired type (<code>findWiredOfType</code> method; see also <a href="http://stackoverflow.com/questions/15373336/is-it-possible-to-access-the-symbol-table-in-a-macro/">this StackOverflow question</a> why the search is limited to the enclosing class). Finally, it assembles a tree corresponding to invoking the constructor with the right arguments:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">Apply<span style="color: #F78811;">&#40;</span>
   Select<span style="color: #F78811;">&#40;</span>New<span style="color: #F78811;">&#40;</span>Ident<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#91;</span><span style="color: #0000ff; font-weight: bold;">class</span><span style="color: #CC66FF;">'s</span> <span style="color: #0000ff; font-weight: bold;">type</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>, nme.<span style="color: #000000;">CONSTRUCTOR</span><span style="color: #F78811;">&#41;</span>, 
   List<span style="color: #F78811;">&#40;</span>Ident<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#91;</span>arg1<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>, Ident<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#91;</span>arg2<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>, ...<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>This concept can be extended in many ways. Firstly, by adding support for sub-typing (now only exact type matches will work). Then, there&#8217;s the ability to define the wirings not only in a class, but also in methods; or extending the search to mixed-in traits, so that you could split the <code>wire</code> definitions among multiple traits (&#8220;modules&#8221;?). Notice that we could also have full flexibility in how we access the wired valued; it could be a <code>val</code>, <code>lazy val</code> or a <code>def</code>. There&#8217;s also support for scoping, factories, singletons, configurations values, &#8230;; for example:</p>
<p>(<em>Dependency Injection of the future!</em>)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">// &quot;scopes&quot;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> a <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>X<span style="color: #F78811;">&#93;</span>
lazy <span style="color: #0000ff; font-weight: bold;">val</span> b <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>Y<span style="color: #F78811;">&#93;</span>
<span style="color: #0000ff; font-weight: bold;">def</span> c <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>Z<span style="color: #F78811;">&#93;</span> 
<span style="color: #0000ff; font-weight: bold;">val</span> d <span style="color: #000080;">=</span> provided<span style="color: #F78811;">&#40;</span>manuallyCreatedInstance<span style="color: #F78811;">&#41;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// override a single dependency</span>
<span style="color: #0000ff; font-weight: bold;">val</span> a <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>X<span style="color: #F78811;">&#93;</span>.<span style="color: #0000ff; font-weight: bold;">with</span><span style="color: #F78811;">&#40;</span>anotherYInstance<span style="color: #F78811;">&#41;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// factories: p1, p2, ... are used in the constructor where needed</span>
<span style="color: #0000ff; font-weight: bold;">def</span> e<span style="color: #F78811;">&#40;</span>p1<span style="color: #000080;">:</span> T, p2<span style="color: #000080;">:</span> U, ...<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> wire<span style="color: #F78811;">&#91;</span>X<span style="color: #F78811;">&#93;</span> 
&nbsp;
<span style="color: #008000; font-style: italic;">// by-name binding for configuration parameters; whenever a class has a</span>
<span style="color: #008000; font-style: italic;">// &quot;maxConnections&quot; constructor argument, this value is used.</span>
<span style="color: #0000ff; font-weight: bold;">val</span> maxConnections <span style="color: #000080;">=</span> conf<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">10</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>A recent project by Guice&#8217;s creator, Bob Lee, goes in the same direction. <a href="http://square.github.com/dagger/">Dagger</a> (mainly targeted at Android as far as I know) uses an annotation processor to generate the wiring code; at runtime, it&#8217;s just plain constructor invocations, no reflection. Similarly here, with the difference that we use Scala&#8217;s macros. </p>
<p>What do you think of such an approach to DI?</p>
<p>Adam </p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/2013/03/dependency-injection-with-scala-macros-auto-wiring/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Veripacks 0.3: importing packages (transitively, of course)</title>
		<link>http://www.warski.org/blog/2013/03/veripacks-0-3-importing-packages-transitively-of-course/</link>
		<comments>http://www.warski.org/blog/2013/03/veripacks-0-3-importing-packages-transitively-of-course/#comments</comments>
		<pubDate>Tue, 12 Mar 2013 19:33:36 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[specification]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[verification]]></category>
		<category><![CDATA[veripacks]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=898</guid>
		<description><![CDATA[Previous versions of Veripacks focused on defining what classes are visible outside of a package hierarchy (exporting). This release focuses on defining what classes can be used inside a package &#8230; <a class="readmore" href="http://www.warski.org/blog/2013/03/veripacks-0-3-importing-packages-transitively-of-course/">Continue Reading &#8594;</a>]]></description>
				<content:encoded><![CDATA[<p>Previous versions of <a href="https://github.com/adamw/veripacks">Veripacks</a> focused on defining what classes are visible outside of a package hierarchy (<em>exporting</em>). This release focuses on defining what classes can be used inside a package hierarchy (<em>importing</em>).</p>
<p>By default, no imports are required. As long as a class is visible (exported), it can be used. This can be now constrained using the <code>@RequiresImport</code> and <code>@Import</code> annotations.</p>
<p>If a package is annotated with <code>@RequiresImport</code>, exported classes from this package (and any child packages) can only be used if the package is imported using <code>@Import</code>. It is illegal to import a package, which doesn&#8217;t require importing. Note that only the package with the annotation can be imported (as a whole), not individual child packages.</p>
<p>As all annotations, the effects of <code>@Import</code> are transitive. That is, if a package (A) is imported in a package (B), its classes are also visible in all child packages (of B). </p>
<p>A simple example, combining the export and import features (which can be used separately as well):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">@RequiresImport
<span style="color: #006699;">package</span> <span style="color: #006699;">foo.bar.p1</span> <span style="color: #009900;">&#123;</span>
  @Export
  <span style="color: #000000; font-weight: bold;">class</span> A <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">class</span> B <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span>
&nbsp;
@<span style="color: #000000; font-weight: bold;">Import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo.bar.p1&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">foo.bar.p2</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">class</span> Test1 <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// OK, p1 is imported and A is exported</span>
    <span style="color: #000000; font-weight: bold;">new</span> B<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// illegal, B is not exported</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">foo.bar.p3</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">class</span> Test2 <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// illegal, p1 is not imported</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Moreover, Veripacks is now available in <a href="http://search.maven.org/#browse%7C1714172582">Maven Central</a>, so using Veripacks just got easier: no need to add a repository to your build definition. See the README for details.</p>
<p>As always the whole code is available on GitHub: <a href="https://github.com/adamw/veripacks">https://github.com/adamw/veripacks</a>.</p>
<p>Have fun!</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/2013/03/veripacks-0-3-importing-packages-transitively-of-course/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>xsbt-proguard-plugin &#8211; taking over, new release</title>
		<link>http://www.warski.org/blog/2013/02/xsbt-proguard-plugin-taking-over-new-release/</link>
		<comments>http://www.warski.org/blog/2013/02/xsbt-proguard-plugin-taking-over-new-release/#comments</comments>
		<pubDate>Tue, 19 Feb 2013 14:20:56 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[plugin]]></category>
		<category><![CDATA[Proguard]]></category>
		<category><![CDATA[SBT]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=889</guid>
		<description><![CDATA[I recently took over the maintenance of xsbt-proguard-plugin from siasia. The plugin lets you create single &#8220;fat&#8221; jars from your project, using the Proguard library. Some of the most important &#8230; <a class="readmore" href="http://www.warski.org/blog/2013/02/xsbt-proguard-plugin-taking-over-new-release/">Continue Reading &#8594;</a>]]></description>
				<content:encoded><![CDATA[<p>I recently took over the maintenance of <a href="https://github.com/adamw/xsbt-proguard-plugin">xsbt-proguard-plugin</a> from <a href="https://github.com/siasia">siasia</a>. The plugin lets you create single &#8220;fat&#8221; jars from your project, using the <a href="http://proguard.sourceforge.net/">Proguard</a> library.</p>
<p>Some of the most important changes:</p>
<ul>
<li>the group id of the plugin is now <code>org.scala-sbt</code>; the plugin is hosted on sbt&#8217;s <a href="http://repo.scala-sbt.org/scalasbt">Ivy</a> repository</li>
<li>proguarding projects with multiple subprojects (modules) now works as expected</li>
<li>supporting sbt 0.12 (only)</li>
<li>updating Proguard to 4.8</li>
</ul>
<p>To use the plugin, add to <code>project/plugins.sbt</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">resolvers +<span style="color: #000080;">=</span> Resolver.<span style="color: #000000;">url</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;sbt-plugin-snapshots&quot;</span>, url<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;http://repo.scala-sbt.org/scalasbt/sbt-plugin-snapshots/&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>Resolver.<span style="color: #000000;">ivyStylePatterns</span><span style="color: #F78811;">&#41;</span>
&nbsp;
addSbtPlugin<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;org.scala-sbt&quot;</span> <span style="color: #000080;">%</span> <span style="color: #6666FF;">&quot;xsbt-proguard-plugin&quot;</span> <span style="color: #000080;">%</span> <span style="color: #6666FF;">&quot;0.1.3-SNAPSHOT&quot;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>So far I only deployed a snapshot version (0.1.3-SNAPSHOT). If there won&#8217;t be any bug reports, or when the bug reports get fixed, I&#8217;ll do a release of a normal version soon.</p>
<p>Please test!</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/2013/02/xsbt-proguard-plugin-taking-over-new-release/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Veripacks 0.2: exporting subpackages</title>
		<link>http://www.warski.org/blog/2013/02/veripacks-0-2-exporting-subpackages/</link>
		<comments>http://www.warski.org/blog/2013/02/veripacks-0-2-exporting-subpackages/#comments</comments>
		<pubDate>Sun, 03 Feb 2013 13:35:31 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[specification]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[verification]]></category>
		<category><![CDATA[veripacks]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=875</guid>
		<description><![CDATA[Veripacks 0.1 allowed to specify which classes should be exported from a package hierarchy, by using a simple @Export annotation, and later verify that the specification is met. Version 0.2 &#8230; <a class="readmore" href="http://www.warski.org/blog/2013/02/veripacks-0-2-exporting-subpackages/">Continue Reading &#8594;</a>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.warski.org/blog/2013/01/veripacks-0-1-verify-package-specifications/">Veripacks 0.1</a> allowed to specify which classes should be exported from a package hierarchy, by using a simple <code>@Export</code> annotation, and later verify that the specification is met. <a href="https://github.com/adamw/veripacks">Version 0.2</a> extends this by allowing to export subpackages as well, using the <code>@ExportSubpackages</code> package annotation.</p>
<p>When exporting a subpackage, only the classes exported by the subpackage will be re-exported (same holds for sub-subpackages). This is in line with Veripacks respecting the hierarchical structure of packages.</p>
<p>(Previously, either all subpackages and all classes where exported &#8211; when no <code>@Export</code> annotations were present, or no subpackages were exported, if at least one <code>@Export</code> annotation was present.)</p>
<p>There are also two new convenience package annotations, <code>@ExportAllClasses</code> and <code>@ExportAllSubpackages</code>, which have pretty self-explaining names.</p>
<p>How does this work? An example will illustrate it best, using some imaginary syntax:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">foo.bar.p1.sub_p1</span> <span style="color: #009900;">&#123;</span>
   @Export
   <span style="color: #000000; font-weight: bold;">class</span> Sub1 <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">foo.bar.p1.sub_p2</span> <span style="color: #009900;">&#123;</span>
   @Export
   <span style="color: #000000; font-weight: bold;">class</span> Sub2 <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">class</span> Sub3 <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Normally the annotation would go to package-info.java</span>
@ExportSubpackages<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sub_p1&quot;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">foo.bar.p1</span> <span style="color: #009900;">&#123;</span>
   @Export
   <span style="color: #000000; font-weight: bold;">class</span> A <span style="color: #009900;">&#123;</span> ... 
      <span style="color: #666666; font-style: italic;">// Legal, Sub1 is exported by sub_p1</span>
      <span style="color: #000000; font-weight: bold;">new</span> sub_p1.<span style="color: #006633;">Sub1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
      <span style="color: #666666; font-style: italic;">// Legal, Sub2 is exported by sub_p2</span>
      <span style="color: #000000; font-weight: bold;">new</span> sub_p2.<span style="color: #006633;">Sub2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
&nbsp;
      <span style="color: #666666; font-style: italic;">// Illegal, Sub3 is not exported by sub_p2</span>
      <span style="color: #000000; font-weight: bold;">new</span> sub_p2.<span style="color: #006633;">Sub3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">foo.bar</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">class</span> B <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Legal, A is exported by p1</span>
      <span style="color: #000000; font-weight: bold;">new</span> p1.<span style="color: #006633;">A</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
&nbsp;
      <span style="color: #666666; font-style: italic;">// Legal, sub_p1 is exported by p1, and Sub1 is exported by sub_p1 </span>
      <span style="color: #000000; font-weight: bold;">new</span> p1.<span style="color: #006633;">sub_p1</span>.<span style="color: #006633;">Sub1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
&nbsp;
      <span style="color: #666666; font-style: italic;">// Illegal, sub_p2 is not exported by p1 </span>
      <span style="color: #000000; font-weight: bold;">new</span> p1.<span style="color: #006633;">sub_p2</span>.<span style="color: #006633;">Sub2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>How can this be useful? Veripacks goes beyond what can be achieved with normal Java access modifiers (especially public and package-private), by extending the meaning of packages to a hierarchical structure. It also aims to replace, in some cases, the need for separate build modules.</p>
<p>What&#8217;s next? As mentioned in the <a href="https://github.com/adamw/veripacks#readme">README</a>, the plans are to add a possibility to require an explicit import of a package, to be able to use its classes. And of course, some IDE support to be able to quickly see what is exported and where would be great.</p>
<p>The release is available in <a href="https://nexus.softwaremill.com/content/repositories/releases/">SoftwareMill&#8217;s maven repository</a>, and the code is available under the Apache2 license on <a href="https://github.com/adamw/veripacks">GitHub</a>.</p>
<p>Have fun!<br />
Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/2013/02/veripacks-0-2-exporting-subpackages/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Dry parameter names</title>
		<link>http://www.warski.org/blog/2013/01/dry-parameter-names/</link>
		<comments>http://www.warski.org/blog/2013/01/dry-parameter-names/#comments</comments>
		<pubDate>Sat, 26 Jan 2013 18:56:34 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Methodology]]></category>
		<category><![CDATA[Object oriented]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=852</guid>
		<description><![CDATA[How often do you see code like this, especially when using dependency injection, single-responsibility principle, and other &#8220;good practices&#8221;? 1 2 3 4 5 class FriendsTimelineReader&#40;userFinder: UserFinder, userStatusReader: UserStatusReader, statusSecurityFilter: &#8230; <a class="readmore" href="http://www.warski.org/blog/2013/01/dry-parameter-names/">Continue Reading &#8594;</a>]]></description>
				<content:encoded><![CDATA[<p>How often do you see code like this, especially when using dependency injection, single-responsibility principle, and other &#8220;good practices&#8221;?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> FriendsTimelineReader<span style="color: #F78811;">&#40;</span>userFinder<span style="color: #000080;">:</span> UserFinder,
   userStatusReader<span style="color: #000080;">:</span> UserStatusReader,
   statusSecurityFilter<span style="color: #000080;">:</span> StatusSecurityFilter<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
   ...
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Note that the parameter names are <strong>exact copies</strong> of the class name. That&#8217;s certainly not <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY</a>!</p>
<p>In Java writing such code is a bit easier than in Scala, since you write the type first, and then the variable name can be auto-completed (the fact that there&#8217;s an auto-complete in IDEs indicates that it&#8217;s a common pattern). In Scala there&#8217;s more typing, but then, less boilerplate around declaring fields/defining a constructor/assigning to fields.</p>
<p>How to avoid that? We can always use cryptic variable names, like &#8220;<code>ssf</code>&#8221; instead of &#8220;<code>statusSecurityFilter</code>&#8220;. But then the whole effort of having nicely named classes to make the code readable, which is quite a hard task, goes to waste.</p>
<p>Of course, variable names are very useful, for example when we have to distinguish between the 10 <code>String</code> parameters that our method takes, create an <code>Int</code> counter, etc. But the more we use specialised wrapper-types (and now possibly even more, with Scala 2.10 introducing <a href="http://docs.scala-lang.org/sips/pending/value-classes.html">value classes</a>) to make our code more type-safe, the more fine-grained our services &#8211; the more often the variable/parameter names will mirror the class name.</p>
<p>Even when there are a couple of instances of a class, often the parameter name contains some <strong>qualifier</strong> + the class name (e.g. given a <code>Person</code> class, <code>personAssociator.createFriends(<strong>firstPerson</strong>, <strong>secondPerson</strong>)</code>). It would be interesting to see some statistics on how often variable name is a mirror of the class name or a suffix &#8211; I suspect it can be a large percentage.</p>
<p>Maybe the next step then in type safety is limiting the complete freedom when naming parameters? Or even, getting rid of the parameter names at all in some cases.</p>
<p>For example. In the snippet from the beginning, we just want to get &#8220;an instance&#8221; of a <code>UserFinder</code>. Most probably there will ever be only one instance of this class in the system, and certainly one will be used at a time by other classes. So why not let the class express that it wants an instance of a class, without having to name it? Let&#8217;s use the <a href="http://en.wikipedia.org/wiki/Article_(grammar)">indefinite article</a> &#8220;a&#8221; as a keyword (we don&#8217;t really care which instance is passed):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> FriendsTimelineReader<span style="color: #F78811;">&#40;</span>a UserFinder,
  a UserStatusReader,
  a StatusSecurityFilter<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>Now, how would you use such a variable inside a class? Let&#8217;s use the definite article &#8220;the&#8221; &#8211; the instance that was given to the class, for example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> user10 <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>the UserFinder<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">findById</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">10</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>Maybe this looks a bit like science-fiction, but wouldn&#8217;t it be convenient? Or maybe this problem is already solved by some mechanisms in other languages?</p>
<p>Adam</p>
<p><em>EDIT 27/01/2013</em>: Extending the above to handle qualifiers:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> Friends<span style="color: #F78811;">&#40;</span>a <span style="color: #CC66FF;">'first</span> Person, a <span style="color: #CC66FF;">'second</span> Person<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
   ...
   <span style="color: #F78811;">&#40;</span>the <span style="color: #CC66FF;">'first</span> Person<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">getName</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
   ...
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/2013/01/dry-parameter-names/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Updating to Scala 2.10: ElasticMQ and scala-macro-debug</title>
		<link>http://www.warski.org/blog/2013/01/updating-to-scala-2-10-elasticmq-and-scala-macro-debug/</link>
		<comments>http://www.warski.org/blog/2013/01/updating-to-scala-2-10-elasticmq-and-scala-macro-debug/#comments</comments>
		<pubDate>Mon, 21 Jan 2013 18:37:17 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[ElasticMQ]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[Macros]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[SQS]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=848</guid>
		<description><![CDATA[As Scala 2.10 was released some time ago, it is high time to update the projects using 2.9: ElasticMQ and scala-macro-debug. Veripacks was already released using 2.10. ElasticMQ Version 0.6.3 &#8230; <a class="readmore" href="http://www.warski.org/blog/2013/01/updating-to-scala-2-10-elasticmq-and-scala-macro-debug/">Continue Reading &#8594;</a>]]></description>
				<content:encoded><![CDATA[<p>As <a href="http://www.scala-lang.org/node/27499">Scala 2.10 was released</a> some time ago, it is high time to update the projects using 2.9: <a href="http://elasticmq.org">ElasticMQ</a> and <a href="https://github.com/adamw/scala-macro-debug">scala-macro-debug</a>. <a href="https://github.com/adamw/veripacks">Veripacks</a> was already released using 2.10.</p>
<h3>ElasticMQ</h3>
<p>Version 0.6.3 of <a href="http://elasticmq.org">ElasticMQ</a> has only one major change &#8211; it is built with Scala 2.10. That meant two code changes:</p>
<ul>
<li>Using <a href="https://github.com/typesafehub/scalalogging">Typesafe&#8217;s Scala Logging</a> instead of slf4s</li>
<li>Using <a href="https://github.com/typesafehub/config">Typesafe&#8217;s Config</a> instead of <a href="https://github.com/twitter/ostrich">Ostrich</a> for stand-alone server</li>
</ul>
<p>The second change is the only one visible to the user. The type-safe configuration that was made possible by Ostrich is great, but it doesn&#8217;t look as if a 2.10-compatible version is coming soon. Typesafe Config uses more traditional configuration files, with reasonable amount of verification (though it&#8217;s a bit funny that migrating to Typesafe&#8217;s library causes the config to stop being type-safe ;) ). Also, a good side of the change is that the server always starts up quickly &#8211; no need to compile the config file. Locally on my laptop start up takes about 0.5 seconds.</p>
<p>As always, the new release should be available in Maven Central soon, and links to download the standalone server packages can be found on the main page.</p>
<h3>scala-macro-debug</h3>
<p>I also updated the <a href="http://www.warski.org/blog/2012/12/starting-with-scala-macros-a-short-tutorial/" title="Starting with Scala Macros: a short tutorial">example Scala macros</a> project, and deployed it to our repositories. If you don&#8217;t want to copy the code, you can now use the debug macro by adding:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">resolvers +<span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;SoftwareMill Snapshots&quot;</span> at <span style="color: #6666FF;">&quot;https://nexus.softwaremill.com/content/repositories/snapshots/&quot;</span>
&nbsp;
libraryDependencies +<span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;com.softwaremill.scalamacrodebug&quot;</span> <span style="color: #000080;">%%</span> <span style="color: #6666FF;">&quot;macros&quot;</span> <span style="color: #000080;">%</span> <span style="color: #6666FF;">&quot;0.0.1-SNAPSHOT&quot;</span></pre></td></tr></table></div>

<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/2013/01/updating-to-scala-2-10-elasticmq-and-scala-macro-debug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
