<?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>Mark Needham &#187; Scala</title>
	<atom:link href="http://www.markhneedham.com/blog/category/scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Mon, 13 Feb 2012 21:25:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Scala: Converting a scala collection to java.util.List</title>
		<link>http://www.markhneedham.com/blog/2012/02/05/scala-converting-a-scala-collection-to-java-util-list/</link>
		<comments>http://www.markhneedham.com/blog/2012/02/05/scala-converting-a-scala-collection-to-java-util-list/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 21:40:33 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3974</guid>
		<description><![CDATA[I&#8217;ve been playing around a little with Goose &#8211; a library for extracting the main body of text from web pages &#8211; and I thought I&#8217;d try converting some of the code to be more scala-esque in style. The API of the various classes/methods is designed so it&#8217;s interoperable with Java code but in order [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around a little with <a href="https://github.com/jiminoc/goose">Goose</a> &#8211; a library for extracting the main body of text from web pages &#8211; and I thought I&#8217;d try converting some of the code to be more scala-esque in style.</p>
<p>The API of the various classes/methods is designed so it&#8217;s interoperable with Java code but in order to use functions like map/filter we need the collection to be a Scala one.</p>
<p>That&#8217;s achieved by importing &#8216;scala.collections.JavaConversions._&#8217; which will apply an implicit conversion to convert the Java collection into a Scala one.</p>
<p>I needed to go back to the Java one again which can be achieved with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">collection</span>.<span style="color: #000000;">JavaConversions</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">val</span> javaCollection <span style="color: #000080;">=</span> seqAsJavaList<span style="color: #F78811;">&#40;</span>Seq<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;abc&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>I also used that function in the <a href="https://github.com/mneedham/goose/blob/94da6fffc30db17a0a7e2060a46fc3317420a83e/src/main/scala/com/gravity/goose/text/StopWords.scala">StopWords.scala</a> object in Goose.</p>
<p>There are a load of other functions available in <a href="https://github.com/scala/scala/blob/master/src/library/scala/collection/JavaConversions.scala">JavaConversions</a> as well for going to a Dictionary, Map, Set and so on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/02/05/scala-converting-a-scala-collection-to-java-util-list/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Scala: Our Retrospective of the benefits/drawbacks</title>
		<link>http://www.markhneedham.com/blog/2011/11/28/scala-our-retrospective-of-the-benefitsdrawbacks/</link>
		<comments>http://www.markhneedham.com/blog/2011/11/28/scala-our-retrospective-of-the-benefitsdrawbacks/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 00:15:00 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3810</guid>
		<description><![CDATA[As the closing part of a Scala Experience Report Liz and I gave at XP Day we detailed a retrospective that we&#8217;d carried out on the project after 3 months where the team outlined the positives/negatives of working with Scala. The team members who were there right at the beginning of the project 3 months [...]]]></description>
			<content:encoded><![CDATA[<p>As the closing part of a <a href="http://www.markhneedham.com/blog/2011/11/24/xp-day-scala-an-experience-report-liz-douglass-and-me/">Scala Experience Report</a> <a href="http://twitter.com/#!/lizdouglass">Liz</a> and I gave at XP Day we detailed a retrospective that we&#8217;d carried out on the project after 3 months where the team outlined the positives/negatives of working with Scala.</p>
<p>The team members who were there right at the beginning of the project 3 months earlier had come up with what they thought the proposed benefits/drawbacks would be so it was quite interesting to look at our thoughts at both times.</p>
<p>Some of this is available in our slides from the talk but <a href="http://twitter.com/natpryce">Nat Pryce</a> suggested it&#8217;d be interesting to post it up in more detail.</p>
<p>We weren&#8217;t aware that we&#8217;d be doing this exercise until the session where we did it and noone looked at the original answers so hopefully some of the potential biases have been removed!</p>
<h4>JUNE</h4>
<ul>
<li>
+++ Increased developer productivity</p>
<ul>
<li>Higher-level language constructs (functional programming, actors, pattern matching, mixins, etc.)</li>
<li>Less code -> less time spent reading code / less defects</li>
<li>Syntax is better suited for writing DSLs (e.g. SBT, Scalatra, ScalaTest, etc.)</li>
</ul>
</li>
<li>+++ Bigger potential to attract talented developers (not using the same old &#8216;boring&#8217; stack)</li>
<li>++  Gentle learning curve for Java devs</li>
<li>+   Built-in support at language-level for handling XML</li>
<li>+   Comes with SBT, a powerful build tool</li>
<li>+   Seamlessly integrates with Java and it&#8217;s ecosystem</li>
<li>+   Runs on the JVM (i.e. no operational concerns)</li>
</ul>
<ul>
<li>&#8212; Bigger potential to screw things up (think: &#8220;with great power comes&#8230;&#8221;)</li>
<li>&#8211;  Tool support is less mature and polished (e.g. IDEs, profilers, metrics, etc.)</li>
<li>-   Community is younger and smaller</li>
<li>-   Scala compiler seems to be slower than Java counterparts</li>
</ul>
<h4>SEPTEMBER</h4>
<p>Liked:</p>
<ul>
<li>+8 Easy to learn</li>
<li>+8 Functional Language (Immutable, closures, etc)</li>
<li>+6 Concise code</li>
<li>+5 SBT power</li>
<li>+4 Case classes</li>
<li>+4 XML support</li>
<li>+4 Java integration</li>
<li>+3 List processing</li>
<li>+3 DSL support</li>
<li>+2 Helpful community (IRC, StackOverflow)</li>
<li>+2 Performance</li>
</ul>
<p>Disliked:</p>
<ul>
<li>-8 IDE support (refactoring, plugin quality)</li>
<li>-5 Slow compiler</li>
<li>-3 Code can become complex to read</li>
<li>-2 Lack of XPath support in XML</li>
<li>-2 SBT complexity</li>
<li>-2 Immature frameworks</li>
</ul>
<p>Quite a few of the expected benefits from June were observed in June, such as having to write less code, functional programming constructs, XML support and the ability to write DSLs.</p>
<p>The community was one benefit which wasn&#8217;t expected &#8211; we&#8217;ve found that every time we get stuck on something we can go on Stack Overflow and find the answer and if that doesn&#8217;t work then someone on IRC will be able to help us almost immediately.</p>
<h4>Complexity</h4>
<p>Our experience with Scala&#8217;s complexity <a href="http://blog.joda.org/2011/11/scala-feels-like-ejb-2-and-other.html">partly matches with that of Stephen Coulbourne</a> who suggests the following:</p>
<blockquote><p>
Scala appears to have attracted developers who are very comfortable with type theory, hard-core functional programming and the mathematical end of programming.</p>
<p>&#8230;</p>
<p>There is also a sense that many in the Scala community struggle to understand how other developers cannot grasp Scala/Type/FP concepts which seem simple to them. This sometimes leads Scala aficionados to castigate those that don&#8217;t understand as lazy or poor quality developers.
</p></blockquote>
<p>We&#8217;ve tried to be reasonably sensible with the language and only used bits of it that the whole team are likely to understand rather than learning some obscure way of solving a problem and checking that in.</p>
<p>On the other hand reading the code of Scala libraries such as <a href="https://github.com/scalaz/scalaz">scalaz</a> or <a href="https://github.com/harrah/xsbt">SBT</a> is something that I, at least, find extremely difficult.</p>
<p>Changing the SBT build files can be quite a scary experience while you try and remember what all the different symbols mean and how they integrate together.</p>
<h4>Learning curve</h4>
<p>The learning curve for Java developers has been a bit of a mixed experience. </p>
<p>When we started working on the project we were effectively writing Java in Scala and we&#8217;ve slowly learnt/introduced more Scala features into our code as time has passed.</p>
<p>I think everyone who has come on that journey has found the transition reasonably okay but we&#8217;ve had other team members who joined later on and went straight into code that they weren&#8217;t familiar with and for them it&#8217;s been more difficult.</p>
<h4>Again, again!</h4>
<p>It will be interesting to see the team&#8217;s thoughts if we do the exercise again 3 more months on.</p>
<p>I would imagine there would be more &#8216;dislikes&#8217; around code complexity now that the code has grown even more in size.</p>
<p>It probably also mean the lack of IDE support becomes more annoying as people want to refactor code and can&#8217;t get the seamless experience that you get when editing Java code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/11/28/scala-our-retrospective-of-the-benefitsdrawbacks/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Java/Scala: Runtime.exec hanging/in &#8216;pipe_w&#8217; state</title>
		<link>http://www.markhneedham.com/blog/2011/11/20/javascala-runtime-exec-hangingin-pipe_w-state/</link>
		<comments>http://www.markhneedham.com/blog/2011/11/20/javascala-runtime-exec-hangingin-pipe_w-state/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 20:20:08 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3795</guid>
		<description><![CDATA[On the system that I&#8217;m currently working on we have a data ingestion process which needs to take zip files, unzip them and then import their contents into the database. As a result we delegate from Scala code to the system unzip command like so: def extract &#123; var command = &#34;unzip %s -d %s&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>On the system that I&#8217;m currently working on we have a data ingestion process which needs to take zip files, unzip them and then import their contents into the database.</p>
<p>As a result we delegate from Scala code to the system unzip command like so:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> extract <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> command <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;unzip %s -d %s&quot;</span> format<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;/file/to/unzip.zip&quot;</span>, <span style="color: #6666FF;">&quot;/place/to/unzip/to&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> process<span style="color: #000080;">:</span> Process <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">null</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">try</span> <span style="color: #F78811;">&#123;</span>
    process <span style="color: #000080;">=</span> Runtime.<span style="color: #000000;">getRuntime</span>.<span style="color: #000000;">exec</span><span style="color: #F78811;">&#40;</span>command<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> exitCode <span style="color: #000080;">=</span> process.<span style="color: #000000;">waitFor</span>
  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">catch</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">case</span> e <span style="color: #000080;">:</span> Exception <span style="color: #000080;">=&gt;</span> <span style="color: #008000; font-style: italic;">// do some stuff</span>
  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">finally</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// close the stream here</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>We ran into a problem where the unzipping process was hanging and executing &#8216;ps&#8217; showed us that the &#8216;unzip&#8217; process was stuck in the &#8216;pipe_w&#8217; (pipe waiting) state which suggested that it was waiting for some sort of input.</p>
<p>After a bit of googling <a href="http://duncan-cragg.org/blog/">Duncan</a> found <a href="http://vyvaks.wordpress.com/2006/05/27/does-runtimeexec-hangs-in-java/">this blog</a> which explained that we needed to process the output stream from our process otherwise it might end up hanging</p>
<p>a.k.a. <a href="http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html">RTFM</a>:</p>
<blockquote><p>
The Runtime.exec methods may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. </p>
<p>The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). </p>
<p>The parent process uses these streams to feed input to and get output from the subprocess. </p>
<p>Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
</p></blockquote>
<p>For most of the zip files we presumably hadn&#8217;t been reaching the limit of the buffer because the list of files being sent to STDOUT by &#8216;unzip&#8217; wasn&#8217;t that high.</p>
<p>In order to get around the problem we needed to gobble up the output stream from unzip like so:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">apache</span>.<span style="color: #000000;">commons</span>.<span style="color: #000000;">io</span>.<span style="color: #000000;">IOUtils</span>
<span style="color: #0000ff; font-weight: bold;">def</span> extract <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> command <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;unzip %s -d %s&quot;</span> format<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;/file/to/unzip.zip&quot;</span>, <span style="color: #6666FF;">&quot;/place/to/unzip/to&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> process<span style="color: #000080;">:</span> Process <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">null</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">try</span> <span style="color: #F78811;">&#123;</span>
    process <span style="color: #000080;">=</span> Runtime.<span style="color: #000000;">getRuntime</span>.<span style="color: #000000;">exec</span><span style="color: #F78811;">&#40;</span>command<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> thisVariableIsNeededToSuckDataFromUnzipDoNotRemove <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;Output: &quot;</span> + IOUtils.<span style="color: #000000;">readLines</span><span style="color: #F78811;">&#40;</span>process.<span style="color: #000000;">getInputStream</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> exitCode <span style="color: #000080;">=</span> process.<span style="color: #000000;">waitFor</span>
  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">catch</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">case</span> e <span style="color: #000080;">:</span> Exception <span style="color: #000080;">=&gt;</span> <span style="color: #008000; font-style: italic;">// do some stuff</span>
  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">finally</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// close the stream here</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>We need to do the same thing with the error stream as well in case &#8216;unzip&#8217; ends up overflowing that buffer as well.</p>
<p>On a couple of blog posts that we came across it was suggested that we should &#8216;gobble up&#8217; the output and error streams on separate threads but we weren&#8217;t sure why exactly that was considered necessary&#8230;</p>
<p>If anyone knows then please let me know in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/11/20/javascala-runtime-exec-hangingin-pipe_w-state/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Scala: scala.xml.SpecialNode: StackOverFlowError</title>
		<link>http://www.markhneedham.com/blog/2011/11/15/scala-scala-xml-specialnode-stackoverflowerror/</link>
		<comments>http://www.markhneedham.com/blog/2011/11/15/scala-scala-xml-specialnode-stackoverflowerror/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 00:26:46 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3791</guid>
		<description><![CDATA[We have some code in our application where we parse reasonably complex XML structures and then sometimes choose to get rid of certain elements from the structure. When we wanted to get rid of an element we replaced that element with a SpecialNode: val emptyNode = new scala.xml.SpecialNode&#40;&#41; &#123; def buildString&#40;sb:StringBuilder&#41; = new StringBuilder&#40;&#41; def [...]]]></description>
			<content:encoded><![CDATA[<p>We have some code in our application where we parse reasonably complex XML structures and then sometimes choose to get rid of certain elements from the structure.</p>
<p>When we wanted to get rid of an element we replaced that element with a <cite><a href="http://www.scala-lang.org/api/current/scala/xml/SpecialNode.html">SpecialNode</a></cite>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> emptyNode <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> scala.<span style="color: #000000;">xml</span>.<span style="color: #000000;">SpecialNode</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> buildString<span style="color: #F78811;">&#40;</span>sb<span style="color: #000080;">:</span>StringBuilder<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> StringBuilder<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> label <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">null</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Unfortunately when you call #text on the node it results in the following exception which we only found out today:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">&gt;</span> emptyNode.<span style="color: #000000;">text</span>
&nbsp;
java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">StackOverflowError</span>
	at scala.<span style="color: #000000;">xml</span>.<span style="color: #000000;">NodeSeq</span>$$anonfun$text$1.<span style="color: #000000;">apply</span><span style="color: #F78811;">&#40;</span>NodeSeq.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">152</span><span style="color: #F78811;">&#41;</span>
	at scala.<span style="color: #000000;">collection</span>.<span style="color: #000000;">TraversableLike</span>$$anonfun$map$1.<span style="color: #000000;">apply</span><span style="color: #F78811;">&#40;</span>TraversableLike.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">194</span><span style="color: #F78811;">&#41;</span>
	at scala.<span style="color: #000000;">collection</span>.<span style="color: #000000;">TraversableLike</span>$$anonfun$map$1.<span style="color: #000000;">apply</span><span style="color: #F78811;">&#40;</span>TraversableLike.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">194</span><span style="color: #F78811;">&#41;</span>
	at scala.<span style="color: #000000;">collection</span>.<span style="color: #000000;">Iterator</span>$class.<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#40;</span>Iterator.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">652</span><span style="color: #F78811;">&#41;</span>
	at scala.<span style="color: #000000;">collection</span>.<span style="color: #000000;">LinearSeqLike</span>$$anon$1.<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#40;</span>LinearSeqLike.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">50</span><span style="color: #F78811;">&#41;</span>
	at scala.<span style="color: #000000;">collection</span>.<span style="color: #000000;">IterableLike</span>$class.<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#40;</span>IterableLike.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">73</span><span style="color: #F78811;">&#41;</span>
	at scala.<span style="color: #000000;">xml</span>.<span style="color: #000000;">NodeSeq</span>.<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#40;</span>NodeSeq.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">43</span><span style="color: #F78811;">&#41;</span>
	at scala.<span style="color: #000000;">collection</span>.<span style="color: #000000;">TraversableLike</span>$class.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>TraversableLike.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">194</span><span style="color: #F78811;">&#41;</span>
	at scala.<span style="color: #000000;">xml</span>.<span style="color: #000000;">NodeSeq</span>.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>NodeSeq.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">43</span><span style="color: #F78811;">&#41;</span>
	at scala.<span style="color: #000000;">xml</span>.<span style="color: #000000;">NodeSeq</span>.<span style="color: #000000;">text</span><span style="color: #F78811;">&#40;</span>NodeSeq.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">152</span><span style="color: #F78811;">&#41;</span>
	at scala.<span style="color: #000000;">xml</span>.<span style="color: #000000;">Node</span>.<span style="color: #000000;">text</span><span style="color: #F78811;">&#40;</span>Node.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">200</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>The way to get around that problem is to override the text method so it returns empty:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> emptyNode <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> scala.<span style="color: #000000;">xml</span>.<span style="color: #000000;">SpecialNode</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> buildString<span style="color: #F78811;">&#40;</span>sb<span style="color: #000080;">:</span>StringBuilder<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> StringBuilder<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> label <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">null</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> text <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">&gt;</span> emptyNode.<span style="color: #000000;">text</span>
res1<span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span></pre></div></div>

<p>It took a seriously long time for us to track down what was going on and that bit of code wasn&#8217;t unit tested.</p>
<p>#fail</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/11/15/scala-scala-xml-specialnode-stackoverflowerror/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scala: Setting default argument for function parameter</title>
		<link>http://www.markhneedham.com/blog/2011/11/08/scala-setting-default-argument-for-function-parameter/</link>
		<comments>http://www.markhneedham.com/blog/2011/11/08/scala-setting-default-argument-for-function-parameter/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 22:46:47 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3776</guid>
		<description><![CDATA[Yesterday I wrote about a problem we&#8217;ve been having with trying to work out how to default a function parameter that we have in one of our methods. Our current version of the code defines the function parameter as implicit which means that if it isn&#8217;t passed in it defaults to Predef.conforms(): def foo&#91;T&#93;&#40;bar: String&#41;&#40;implicit [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I wrote about a problem we&#8217;ve been having with <a href="http://www.markhneedham.com/blog/2011/11/06/scala-which-implicit-conversion-is-being-used/">trying to work out how to default a function parameter that we have in one of our methods</a>.</p>
<p>Our current version of the code defines the function parameter as implicit which means that if it isn&#8217;t passed in it defaults to <cite><a href="http://www.scala-lang.org/api/current/index.html#scala.Predef$$$less$colon$less">Predef.conforms()</a></cite>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> foo<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>bar<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> blah<span style="color: #000080;">:</span><span style="color: #F78811;">&#40;</span>String <span style="color: #000080;">=&gt;</span> T<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span> 
  println<span style="color: #F78811;">&#40;</span>blah<span style="color: #F78811;">&#40;</span>bar<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span> 
  bar 
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>It&#8217;s not entirely clear just from reading the code where the implicit value is coming from so we want to try and make the code a bit more expressive.</p>
<p>The way we wanted to do this was by making &#8216;blah&#8217; have a default value rather than making it implicit.</p>
<p>Our equivalent to <cite>Predef.conforms()</cite> is the <cite><a href="http://stackoverflow.com/questions/1797502/is-there-a-scala-identity-function">identity</a></cite> function and our first attempt at defaulting the parameter looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> foo<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>bar<span style="color: #000080;">:</span> String, blah<span style="color: #000080;">:</span><span style="color: #F78811;">&#40;</span>String <span style="color: #000080;">=&gt;</span> T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> identity <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span> 
  println<span style="color: #F78811;">&#40;</span>blah<span style="color: #F78811;">&#40;</span>bar<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span> 
  bar 
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Unfortunately when we try to use that function without providing the second argument we get the following exception:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">scala&gt; foo(&quot;mark&quot;)
&lt;console&gt;:18: error: polymorphic expression cannot be instantiated to expected type;
 found   : [T](Nothing) =&gt; Nothing
 required: (String) =&gt; ?
Error occurred in an application involving default arguments.
       foo(&quot;mark&quot;)</pre></div></div>

<p>From what I understand the compiler is unable to infer the type of the input parameter, a problem we can fix by explicitly specifying that:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> foo<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>bar<span style="color: #000080;">:</span> String, blah<span style="color: #000080;">:</span><span style="color: #F78811;">&#40;</span>String <span style="color: #000080;">=&gt;</span> T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> identity<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span> println<span style="color: #F78811;">&#40;</span>blah<span style="color: #F78811;">&#40;</span>bar<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span> bar <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>We can then either choose to provide a function:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> foo<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;mark&quot;</span>, <span style="color: #000080;">_</span> + <span style="color: #6666FF;">&quot;needham&quot;</span><span style="color: #F78811;">&#41;</span>
markneedham
res17<span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> mark</pre></div></div>

<p>&#8230;or not:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> foo<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;mark&quot;</span><span style="color: #F78811;">&#41;</span>
mark
res16<span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> mark</pre></div></div>

<p>This solves the problem for this simple example but an interesting problem that we then ran into is that we actually had overloaded versions of the method in question and only one overload is allowed to specify default arguments <a href="http://www.scala-lang.org/sites/default/files/sids/rytz/Mon,%202009-11-09,%2017:29/named-args.pdf">as per the spec</a>.</p>
<p>Each overload actually takes in different parameter types so one way to get around this problem would be to make some of the parameters optional and then default them to None.</p>
<p>At the moment we&#8217;ve ended up leaving the implicit conversion in because the change is a bit bigger in nature than antiticpated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/11/08/scala-setting-default-argument-for-function-parameter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scala: Which implicit conversion is being used?</title>
		<link>http://www.markhneedham.com/blog/2011/11/06/scala-which-implicit-conversion-is-being-used/</link>
		<comments>http://www.markhneedham.com/blog/2011/11/06/scala-which-implicit-conversion-is-being-used/#comments</comments>
		<pubDate>Sun, 06 Nov 2011 21:25:06 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3774</guid>
		<description><![CDATA[Last week my colleague Pat created a method which had a parameter which he wanted to make optional so that consumers of the API wouldn&#8217;t have to provide it if they didn&#8217;t want to. We ended up making the method take in an implicit value such that the method signature looked a bit like this: [...]]]></description>
			<content:encoded><![CDATA[<p>Last week my colleague <a href="http://twitter.com/patforna">Pat</a> created a method which had a parameter which he wanted to make optional so that consumers of the API wouldn&#8217;t have to provide it if they didn&#8217;t want to.</p>
<p>We ended up making the method take in an implicit value such that the method signature looked a bit like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> foo<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> blah<span style="color: #000080;">:</span><span style="color: #F78811;">&#40;</span>String <span style="color: #000080;">=&gt;</span> T<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
  println<span style="color: #F78811;">&#40;</span>blah<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;mark&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #6666FF;">&quot;foo&quot;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>We can call foo with or without an argument:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> foo <span style="color: #F78811;">&#123;</span> x <span style="color: #000080;">=&gt;</span> x + <span style="color: #6666FF;">&quot; Needham&quot;</span> <span style="color: #F78811;">&#125;</span> 
mark Needham
res16<span style="color: #000080;">:</span> java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">String</span> <span style="color: #000080;">=</span> foo</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> foo 
mark
res17<span style="color: #000080;">:</span> java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">String</span> <span style="color: #000080;">=</span> foo</pre></div></div>

<p>In the second case it seems like the function is defaulting to an identity function of some sorts since the same value we pass to it is getting printed out. </p>
<p>We figured that it was probably using one of the implicit conversions in <cite>Predef</cite> but weren&#8217;t sure which one.</p>
<p>I asked about this on the Scala IRC channel and Heikki Vesalainen suggested running <cite>scala</cite> with the &#8216;-print&#8217; flag to work it out.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala -print</pre></div></div>

<p>The output is pretty verbose but having defined foo as above this is some of the output we get when calling it:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> foo
<span style="color: #F78811;">&#91;</span><span style="color: #F78811;">&#91;</span>syntax trees at end of cleanup<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span><span style="color: #008000; font-style: italic;">// Scala source: &lt;console&gt;</span>
<span style="color: #0000ff; font-weight: bold;">package</span> $line2 <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">final</span> <span style="color: #0000ff; font-weight: bold;">object</span> $read <span style="color: #0000ff; font-weight: bold;">extends</span> java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">Object</span> <span style="color: #0000ff; font-weight: bold;">with</span> ScalaObject <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> <span style="color: #0000ff; font-weight: bold;">object</span> $line2.$read <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      $read.<span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
      <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span><span style="color: #000080;">;</span>
  <span style="color: #0000ff; font-weight: bold;">final</span> <span style="color: #0000ff; font-weight: bold;">object</span> $read$$iw$$iw <span style="color: #0000ff; font-weight: bold;">extends</span> java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">Object</span> <span style="color: #0000ff; font-weight: bold;">with</span> ScalaObject <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">private</span><span style="color: #F78811;">&#91;</span><span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#93;</span> <span style="color: #0000ff; font-weight: bold;">val</span> res0<span style="color: #000080;">:</span> java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">String</span> <span style="color: #000080;">=</span> <span style="color: #000080;">_;</span>
    <span style="color: #000080;">&lt;</span>stable<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&lt;</span>accessor<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">def</span> res0<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">String</span> <span style="color: #000080;">=</span> $read$$iw$$iw.<span style="color: #0000ff; font-weight: bold;">this</span>.<span style="color: #000000;">res0</span><span style="color: #000080;">;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> <span style="color: #0000ff; font-weight: bold;">object</span> $line2.$read$$iw$$iw <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      $read$$iw$$iw.<span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
      $read$$iw$$iw.<span style="color: #0000ff; font-weight: bold;">this</span>.<span style="color: #000000;">res0</span> <span style="color: #000080;">=</span> $line1.$read$$iw$$iw.<span style="color: #000000;">foo</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">&lt;</span>strong<span style="color: #000080;">&gt;</span>scala.<span style="color: #0000ff; font-weight: bold;">this</span>.<span style="color: #000000;">Predef</span>.<span style="color: #000000;">conforms</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">&lt;</span>/strong<span style="color: #000080;">&gt;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
      <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span><span style="color: #000080;">;</span>
  <span style="color: #0000ff; font-weight: bold;">final</span> <span style="color: #0000ff; font-weight: bold;">object</span> $read$$iw <span style="color: #0000ff; font-weight: bold;">extends</span> java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">Object</span> <span style="color: #0000ff; font-weight: bold;">with</span> ScalaObject <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> <span style="color: #0000ff; font-weight: bold;">object</span> $line2.$read$$iw <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      $read$$iw.<span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
      <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>I&#8217;ve highlighted the call to <cite>Predef.conforms()</cite> which is the implicit conversion that&#8217;s been substituted into &#8216;foo&#8217;.</p>
<p>It&#8217;s defined like so:</p>
<p><a href="http://www.scala-lang.org/api/current/index.html#scala.Predef$$$less$colon$less">Predef.scala</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>350
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> conforms<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span><span style="color: #000080;">:</span> A <span style="color: #000080;">&lt;:&lt;</span> A <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> <span style="color: #F78811;">&#40;</span>A <span style="color: #000080;">&lt;:&lt;</span> A<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">def</span> apply<span style="color: #F78811;">&#40;</span>x<span style="color: #000080;">:</span> A<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> x <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;m not sure where that would be legitimately used but the comments just above it suggest the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">An instance of `A &lt;:&lt; B` witnesses that `A` is a subtype of `B`.</pre></div></div>

<p>This is probably a misuse of implcits and we intend to replace the implicit in our code with a default function value but it was interesting investigating where the implicit had come from!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/11/06/scala-which-implicit-conversion-is-being-used/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Scala: Option.isDefined as the new null check</title>
		<link>http://www.markhneedham.com/blog/2011/11/01/scala-option-isdefined-as-the-new-null-check/</link>
		<comments>http://www.markhneedham.com/blog/2011/11/01/scala-option-isdefined-as-the-new-null-check/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 00:58:45 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3772</guid>
		<description><![CDATA[One cool thing about using Scala on my current project is that we don&#8217;t have nulls anywhere in our code, instead when something may or may not be there we make use of the Option type. Unfortunately what we&#8217;ve (heavily contributed by me) ended up with in our code base is repeated use of the [...]]]></description>
			<content:encoded><![CDATA[<p>One cool thing about using Scala on my current project is that we don&#8217;t have nulls anywhere in our code, instead when something may or may not be there we make use of the Option type.</p>
<p>Unfortunately what we&#8217;ve (heavily contributed by me) ended up with in our code base is repeated use of the <cite><a href="http://www.scala-lang.org/api/rc/scala/Option.html">isDefined</a></cite> method whenever we want to make a decision depending on whether or not the option is populated.</p>
<p>For example the following is quite common:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> Foo<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">val</span> bar<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> foo <span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>Foo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Some<span style="color: #F78811;">&#40;</span>Foo<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;mark&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> bar <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">if</span><span style="color: #F78811;">&#40;</span>foo.<span style="color: #000000;">isDefined</span><span style="color: #F78811;">&#41;</span> Some<span style="color: #F78811;">&#40;</span>foo.<span style="color: #000000;">get</span>.<span style="color: #000000;">bar</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">else</span> None
bar<span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Some<span style="color: #F78811;">&#40;</span>mark<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>We can actually get rid of the if statement by making use of <cite>collect</cite> instead:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> bar <span style="color: #000080;">=</span> foo.<span style="color: #000000;">collect</span> <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">case</span> f <span style="color: #000080;">=&gt;</span> f.<span style="color: #000000;">bar</span> <span style="color: #F78811;">&#125;</span> 
bar<span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Some<span style="color: #F78811;">&#40;</span>mark<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>And if foo is <cite>None</cite>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> foo <span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>Foo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> None
<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> bar <span style="color: #000080;">=</span> foo.<span style="color: #000000;">collect</span> <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">case</span> f <span style="color: #000080;">=&gt;</span> f.<span style="color: #000000;">bar</span> <span style="color: #F78811;">&#125;</span> 
bar<span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> None</pre></div></div>

<p>The code is now simpler and as long as you understand <cite>collect</cite> then it&#8217;s easier to understand as well.</p>
<p>Another quite common example would be something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> Foo<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">val</span> bar<span style="color: #000080;">:</span>Option<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> foos <span style="color: #000080;">=</span> List<span style="color: #F78811;">&#40;</span>Foo<span style="color: #F78811;">&#40;</span>Some<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;mark&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>, Foo<span style="color: #F78811;">&#40;</span>None<span style="color: #F78811;">&#41;</span>, Foo<span style="color: #F78811;">&#40;</span>Some<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;needham&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
foos<span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span>Foo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> List<span style="color: #F78811;">&#40;</span>Foo<span style="color: #F78811;">&#40;</span>Some<span style="color: #F78811;">&#40;</span>mark<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>, Foo<span style="color: #F78811;">&#40;</span>None<span style="color: #F78811;">&#41;</span>, Foo<span style="color: #F78811;">&#40;</span>Some<span style="color: #F78811;">&#40;</span>needham<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">&gt;</span> foos.<span style="color: #000000;">filter</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">bar</span>.<span style="color: #000000;">isDefined</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">bar</span>.<span style="color: #000000;">get</span> + <span style="color: #6666FF;">&quot; awesome&quot;</span><span style="color: #F78811;">&#41;</span>
res23<span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span>java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">String</span><span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> List<span style="color: #F78811;">&#40;</span>mark awesome, needham awesome<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>Which we can simplify down to:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">foos.<span style="color: #000000;">collect</span> <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">case</span> Foo<span style="color: #F78811;">&#40;</span>Some<span style="color: #F78811;">&#40;</span>bar<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> bar + <span style="color: #6666FF;">&quot; awesome&quot;</span> <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>When I was playing around with F# a couple of years ago I learnt that wherever possible I should try and keep chaining functions together rather than breaking the code up into conditionals and I think the same applies here.</p>
<p>There are loads of methods available on <cite><a href="http://www.scala-lang.org/api/rc/scala/collection/TraversableLike.html">TraversableLike</a></cite> to help us achieve this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/11/01/scala-option-isdefined-as-the-new-null-check/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Scala: Adding logging around a repository</title>
		<link>http://www.markhneedham.com/blog/2011/10/25/scala-adding-logging-around-a-repository/</link>
		<comments>http://www.markhneedham.com/blog/2011/10/25/scala-adding-logging-around-a-repository/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 21:19:22 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3760</guid>
		<description><![CDATA[We wanted to add some logging around one of our repositories to track how many times users were trying to do various things on the application and came across a cool blog post explaining how we might be able to do this. We ended up with the following code: class BarRepository &#123; def all: Seq&#91;Bar&#93; [...]]]></description>
			<content:encoded><![CDATA[<p>We wanted to add some logging around one of our repositories to track how many times users were trying to do various things on the application and came across <a href="http://yacode.blogspot.com/2011/07/dynamic-proxy-using-scala-implicit.html">a cool blog post explaining how we might be able to do this</a>.</p>
<p>We ended up with the following 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> BarRepository <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> all<span style="color: #000080;">:</span> Seq<span style="color: #F78811;">&#91;</span>Bar<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Seq<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> find<span style="color: #F78811;">&#40;</span>barId<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> Bar <span style="color: #000080;">=</span> Bar<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;myBar&quot;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> TrackService<span style="color: #F78811;">&#40;</span>barRepository<span style="color: #000080;">:</span>BarRepository<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> all <span style="color: #000080;">:</span> Seq<span style="color: #F78811;">&#91;</span>Bar<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span> 
    <span style="color: #0000ff; font-weight: bold;">var</span> bars <span style="color: #000080;">=</span> barRepository.<span style="color: #000000;">all</span><span style="color: #000080;">;</span> 
    println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;tracking all bars&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span> 
    bars 
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> trackServiceToBarRepository<span style="color: #F78811;">&#40;</span>t<span style="color: #000080;">:</span>TrackService<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> BarRepository <span style="color: #000080;">=</span> t.<span style="color: #000000;">barRepository</span></pre></div></div>

<p>We can then use it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> service <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> TrackService<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> BarRepository<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
service<span style="color: #000080;">:</span> TrackService <span style="color: #000080;">=</span> TrackService<span style="color: #000080;">@</span>4e5394c
&nbsp;
scala<span style="color: #000080;">&gt;</span> service.<span style="color: #000000;">all</span>
tracking all bars
res6<span style="color: #000080;">:</span> Seq<span style="color: #F78811;">&#91;</span>Bar<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> List<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>If a method doesn&#8217;t exist on <cite>TrackService</cite> then the implicit conversion ensures that the appropriate method will be called on <cite>BarRepository</cite> directly:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> service.<span style="color: #000000;">find</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;mark&quot;</span><span style="color: #F78811;">&#41;</span>
res7<span style="color: #000080;">:</span> Bar <span style="color: #000080;">=</span> Bar<span style="color: #F78811;">&#40;</span>myBar<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>I came across another way to achieve the same results by <a href="http://java.dzone.com/articles/real-world-scala-managing-cros">making use of traits</a> although we&#8217;d need to change our design a little bit to achieve this pattern:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> IProvideBars <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> all <span style="color: #000080;">:</span> Seq<span style="color: #F78811;">&#91;</span>Bar<span style="color: #F78811;">&#93;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> find<span style="color: #F78811;">&#40;</span>barId<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> Bar
<span style="color: #F78811;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> BarRepository <span style="color: #0000ff; font-weight: bold;">extends</span> IProvideBars <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> all<span style="color: #000080;">:</span> Seq<span style="color: #F78811;">&#91;</span>Bar<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Seq<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> find<span style="color: #F78811;">&#40;</span>barId<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> Bar <span style="color: #000080;">=</span> Bar<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;myBar&quot;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> Tracking <span style="color: #0000ff; font-weight: bold;">extends</span> IProvideBars <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">abstract</span> <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> all <span style="color: #000080;">:</span> Seq<span style="color: #F78811;">&#91;</span>Bar<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span> 
    <span style="color: #0000ff; font-weight: bold;">val</span> bars <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #000000;">all</span><span style="color: #000080;">;</span>
    println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;tracking all bars&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span> 
    bars 
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> b <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> BarRepository<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">with</span> Tracking
b<span style="color: #000080;">:</span> BarRepository <span style="color: #0000ff; font-weight: bold;">with</span> Tracking <span style="color: #000080;">=</span> $anon$1<span style="color: #000080;">@</span>ddc652f
&nbsp;
scala<span style="color: #000080;">&gt;</span> b.<span style="color: #000000;">all</span>
tracking all bars
res8<span style="color: #000080;">:</span> Seq<span style="color: #F78811;">&#91;</span>Bar<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> List<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/10/25/scala-adding-logging-around-a-repository/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Scala: Creating an Xml element with an optional attribute</title>
		<link>http://www.markhneedham.com/blog/2011/10/25/scala-creating-an-xml-element-with-an-optional-attribute/</link>
		<comments>http://www.markhneedham.com/blog/2011/10/25/scala-creating-an-xml-element-with-an-optional-attribute/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 20:38:52 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3758</guid>
		<description><![CDATA[We have a lot of Xml in our application and one of the things that we need to do reasonably frequently in our test code is create elements which have optional attributes on them. Our simple first approach looked like this: def createElement&#40;attribute: Option&#91;String&#93;&#41; = if&#40;attribute.isDefined&#41; &#60;p bar=&#123;attribute.get&#125; /&#62; else &#60;p /&#62; That works but [...]]]></description>
			<content:encoded><![CDATA[<p>We have a lot of Xml in our application and one of the things that we need to do reasonably frequently in our test code is create elements which have optional attributes on them.</p>
<p>Our simple first approach looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> createElement<span style="color: #F78811;">&#40;</span>attribute<span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">if</span><span style="color: #F78811;">&#40;</span>attribute.<span style="color: #000000;">isDefined</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>p bar<span style="color: #000080;">=</span><span style="color: #F78811;">&#123;</span>attribute.<span style="color: #000000;">get</span><span style="color: #F78811;">&#125;</span> /<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #000080;">&lt;</span>p /<span style="color: #000080;">&gt;</span></pre></div></div>

<p>That works but it always seemed like we should be able to do it in a simpler way.</p>
<p>Our first attempt was this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> createElement<span style="color: #F78811;">&#40;</span>attribute<span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #000080;">&lt;</span>p bar<span style="color: #000080;">=</span><span style="color: #F78811;">&#123;</span>attribute<span style="color: #F78811;">&#125;</span> /<span style="color: #000080;">&gt;</span></pre></div></div>

<p>But that ends up in a compilation error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">error: overloaded method constructor UnprefixedAttribute with alternatives:
  (key: String,value: Option[Seq[scala.xml.Node]],next: scala.xml.MetaData)scala.xml.UnprefixedAttribute &lt;and&gt;
  (key: String,value: String,next: scala.xml.MetaData)scala.xml.UnprefixedAttribute &lt;and&gt;
  (key: String,value: Seq[scala.xml.Node],next1: scala.xml.MetaData)scala.xml.UnprefixedAttribute
 cannot be applied to (java.lang.String, Option[String], scala.xml.MetaData)
       def createElement1(attribute: Option[String]) = &lt;p bar={attribute} /&gt;</pre></div></div>

<p>We really need to extract the string value from the option if there is one and not do anything if there isn&#8217;t one but with the above approach we try to shove an option in as the attribute value. Unfortunately there isn&#8217;t an overload of the constructor which lets us do that.</p>
<p>Eventually one of my colleagues suggested we try passing null in as the attribute value if we had a None option:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> createElement<span style="color: #F78811;">&#40;</span>attribute<span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #000080;">&lt;</span>p bar<span style="color: #000080;">=</span><span style="color: #F78811;">&#123;</span>attribute.<span style="color: #000000;">getOrElse</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#125;</span> /<span style="color: #000080;">&gt;</span></pre></div></div>

<p>Which works pretty well:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">scala&gt; createElement(Some(&quot;mark&quot;))
res0: scala.xml.Elem = &lt;p bar=&quot;mark&quot;&gt;&lt;/p&gt;
&nbsp;
scala&gt; createElement(None)
res1: scala.xml.Elem = &lt;p &gt;&lt;/p&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/10/25/scala-creating-an-xml-element-with-an-optional-attribute/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Scala: Replacing a trait with a fake one for testing</title>
		<link>http://www.markhneedham.com/blog/2011/09/25/scala-replacing-a-trait-with-a-fake-one-for-testing/</link>
		<comments>http://www.markhneedham.com/blog/2011/09/25/scala-replacing-a-trait-with-a-fake-one-for-testing/#comments</comments>
		<pubDate>Sun, 25 Sep 2011 10:24:20 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3736</guid>
		<description><![CDATA[We recently wanted to replace a trait mixed into one of our classes with a fake version to make it easier to test but forgot how exactly to do that! The class is roughly like this: trait Foo &#123; def foo : String = &#34;real foo&#34; &#125; class Mark extends Foo &#123;&#125; We originally tried [...]]]></description>
			<content:encoded><![CDATA[<p>We recently wanted to replace a trait mixed into one of our classes with a fake version to make it easier to test but forgot how exactly to do that!</p>
<p>The class is roughly like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> Foo <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">def</span> foo <span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;real foo&quot;</span> <span style="color: #F78811;">&#125;</span> 
<span style="color: #0000ff; font-weight: bold;">class</span> Mark <span style="color: #0000ff; font-weight: bold;">extends</span> Foo <span style="color: #F78811;">&#123;</span><span style="color: #F78811;">&#125;</span></pre></div></div>

<p>We originally tried to replace it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> BrokenFakeFoo <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">def</span> foo <span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;broken fake foo&quot;</span> <span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> m <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Mark <span style="color: #0000ff; font-weight: bold;">with</span> BrokenFakeFoo</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">error: overriding method foo in trait Foo of type =&gt; String;
 method foo in trait BrokenFakeFoo of type =&gt; String needs `override' modifier
       val m = new Mark with BrokenFakeFoo</pre></div></div>

<p>If <cite>m</cite> compiled it would have two versions of <cite>foo</cite> but it wouldn&#8217;t know which one to use, hence the error message.</p>
<p>Attempt two was this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> BrokenFakeFoo <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> foo <span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;broken fake foo&quot;</span> <span style="color: #F78811;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">error: method foo overrides nothing
       trait BrokenFakeFoo { override def foo : String = &quot;broken fake foo&quot; }</pre></div></div>

<p>As <a href="https://github.com/uday-rayala">Uday</a> pointed out, what we actually need to do is make our fake trait extend the original one and then override the method.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> FakeFoo <span style="color: #0000ff; font-weight: bold;">extends</span> Foo <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> foo <span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;fake foo&quot;</span> <span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> m <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Mark <span style="color: #0000ff; font-weight: bold;">with</span> FakeFoo</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">m.foo
&gt; res5: String = fake foo</pre></div></div>

<p>Since <cite>FakeFoo</cite> is the right most of the traits mixed into <cite>Mark</cite> its <cite>foo</cite> method will be used over the <cite>Foo</cite> one mixed into <cite>Mark</cite> on its class definition.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/09/25/scala-replacing-a-trait-with-a-fake-one-for-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

