Code « Matt’s Blog

Matt’s Blog Just another weblog

29Mar/14Off

sbt-dependency-graph in project/build.scala

In an earlier post, I mentioned how to include sbt-dependency-graph in your Scala project. I found the documentation a bit confusing on how to add this capability when using project/build.scala. Here is a reference for how this is done: see this link.

Filed under: scala No Comments
8Dec/13Off

My Scala Notes

I've been working with Scala recently and just wanted to make this post about some tips that I'd like to remember.

Annotating Test Classes
When writing Scala tests, it's convenient to annotate them such that Eclipse is able to recognize how to execute the test from the "Run" menu. We can enable this by annotating the test class with a @RunWith statement. The IDE unfortunately doesn't seem to recognize that @RunWith and JUnitRunner are required imports when subsequently performing an "organize imports" operation. As a result, it's best to specify the fully qualified class name as shown on line two.

@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
class MyScalaTest extends FunSuite {
 
...
 
}

Testing Exceptions in scalatest
Below is an example of verifying that code under test throws an exception.

test("throws an exception") {
    intercept[Exception] {
        throw new Exception("hi")
    }
}

Working with Eclipse on Windows and Linux Console
I like to use eclipse, emacs, and the console during coding sessions. On my Windows machine I run Eclipse and access the source files over a network drive. This setup requires that my development life cycle be cross-platform compatible. To enable this workflow, it's convenient for sbt to generate the eclipse project file with all the required dependencies. This is done by adding,

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.2.0")

to the plugins.sbt file and running sbt eclipse. Note, the version of the plugin will vary depending on your Scala version. It's best to visit the project page and try the latest version. Now, it's also helpful to add the following configuration to your build.sbt file,

// puts library dependencies in the lib_managed directory
retrieveManaged := true

This will download all dependencies to the project directory and the eclipse .project file will be able to reference them locally (as opposed to the ivy repo).

mvn dependency:tree in sbt?
There will come a time when you'll need to fight through dependency issues. One tool I frequently use is mvn dependency:tree. The equivalent of this command in sbt is enabled by including the following plugin,

addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.7.4")

and the following to build.sbt,

net.virtualvoid.sbt.graph.Plugin.graphSettings

The dependency tree can be generated by typing,

sbt dependency-tree

See this page for some more info.

Tagged as: No Comments
27Nov/11Off

Clean implementation of static class instances

Sometimes an application requires a single static instance of a class for the duration of the life of the application. This is typically done by following the Single Design Pattern. One way to demonstrate such an implementation in C# is as follows,

public class Dog {
    private static Dog m_Instance = null;
    public static Dog Instance {
        get {
            if (m_Instance == null) {
               m_Instance = new Dog();
            }
            return m_Instance;
        }
    }
    // Dog implementation follows ...
}

However, can we do better? For instance, what if we have a number of classes which should have a single static instance? We would have to repeat the above code within each class to provide an Instance property. I was recently presented with this exact problem and realized that there is something quite common in the Instance implementation.

In fact, just about the entirety of the code is common except the invocation of the constructor for a particular type. Using generics and reflection we can abstract the Instance property by providing a single abstract class that implements the Instance property for any class type with a default constructor.

The following is a neat implementation of just that.

    public abstract class StaticInstance<T> where T : class, new()
    {
        private static T instance = null;
        public static T Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = Activator.CreateInstance<T>();
                }
                return instance;
            }
        }
    }

Now, we can implement Dog as follows,

public class Dog : StaticInstance<Dog> {
    public Dog() { ... }
    // Dog implementation follows ...
}

which gives the Dog class the Instance property. Neat!

Filed under: Code No Comments